How to Convert a List of Booleans to Integers

Problem Formulation and Solution Overview In this article, you’ll learn how to convert a List of Booleans to Integers. In Python, the Boolean is a built-in data type. These values represent True (1) or False (0). Also referred to as Truthy or Falsy values. In this article, we will articulate how these values behave. To … Read more

Python Convert String to CSV File

Problem Formulation Given a Python string: πŸ’¬ Question: How to convert the string to a CSV file in Python? The desired output is the CSV file: ‘my_file.csv’: a,b,c 1,2,3 9,8,7 Simple Vanilla Python Solution To convert a multi-line string with comma-separated values to a CSV file in Python, simply write the string in a file … Read more

How to Create a List from a Comma-Separated String

Problem Formulation and Solution Overview In this article, you’ll learn how to convert a comma-separated string into a List. Fergus, a 10-year-old boy, is learning to code with Python. As homework, the teacher has asked the class to create a comma-separated string and convert this string into a list. Fergus needs your help. πŸ’¬ Question: … Read more

How to Split a List in Half in 5 Ways

Problem Formulation and Solution Overview In this article, you’ll learn how to split a Python List in half. To make it more fun, we have the following running scenario: Lisa is writing a report concerning Population growth for three (3) countries (the US, UK, and Germany) between 2021-2022. However, she saved it as one list … Read more

Python – Convert CSV to List of Lists

Problem Formulation Given a CSV file (e.g., stored in the file with name ‘my_file.csv’). INPUT: file ‘my_file.csv’ 9,8,7 6,5,4 3,2,1 Challenge: How to convert it to a list of lists (=nested list), i.e., putting the row values into the inner lists? OUTPUT: Python list of lists [[9, 8, 7], [6, 5, 4], [3, 2, 1]] … Read more

Convert CSV to Dictionary in Python

The best way to convert a CSV file to a Python dictionary is to create a CSV file object f using open(“my_file.csv”) and pass it in the csv.DictReader(f) method. The return value is an iterable of dictionaries, one per row in the CSV file, that maps the column header from the first row to the … Read more