Problem Formulation
What is the most Pythonic way to convert a CSV file to
- a list of dictionaries,
- a list of tuples, and
- a list of lists?
Let’s go over those challenges one by one.
π« Note: I only show you the most Pythonic solution to each problem — based on my personal opinion — and refer you to more detailed tutorials where it applies.
Convert CSV to List of Dictionaries in Python
Hereβs the content of an example CSV file "my_file.csv"
used in our code snippet below:
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
How to convert this file to a list of dictionaries in Python that looks like as follows?
[{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}
{'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}
{'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}]
Hereβs the code to convert that CSV file to a list of dictionaries, one dictionary per row by using the csv.DictReader(file)
function:
import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.DictReader(f) lst = list(*reader)
A dictionary is a data structure that maps keys to values.
The result of the previous code snippet is a list of dictionaries. The first row of the CSV is used as a header to determine the keys of the dictionary that are mapped to the values defined in the individual rows of the CSV file:
[{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'} {'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'} {'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}]
The csv.DictReader(f)
method takes a file object f
as an input argument. So, you first need to open the file using the built-in Python open()
function.
π Learn More: Feel free to check our detailed guide with more alternatives and variants of this problem in this article on the Finxter blog (opens in new tab).
How to Convert a CSV File to a List of Lists in Python?
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]]
To read a CSV to a nested list in pure Python, open the file using open('my_file.csv')
, read all lines into a variable using f.readlines()
. Now, iterate over all lines, strip them from whitespace using strip()
, and split them on the delimiter ','
using split(',')
.
You can accomplish this in a simple list comprehension statement like so:
csv_filename = 'my_file.csv' with open(csv_filename) as f: lines = f.readlines() lst = [line.strip().split(',') for line in lines]
The output is:
print(lst) # [['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]
π Learn More: Feel free to check our detailed guide with more alternatives and variants of this problem in this article on the Finxter blog (opens in new tab).
Convert CSV to List of Tuples in Python
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 the CSV file to a list of tuples, i.e., putting the row values into the inner tuples?
OUTPUT: Python list of tuples
[(9, 8, 7), (6, 5, 4), (3, 2, 1)]
To convert a CSV file 'my_file.csv'
into a list of tuples in Python, use csv.reader(file_obj)
to create a CSV file reader that holds an iterable of lists, one per row.
Now, use the list(tuple(line) for line in reader)
expression with a generator expression to convert each inner list to a tuple.
Hereβs a simple example that converts our CSV file to a nested list using this approach:
import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(tuple(line) for line in reader)
The output is:
print(lst) # [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]
π Learn More: Feel free to check our detailed guide with more alternatives and variants of this problem in this article on the Finxter blog (opens in a new tab).
More Python CSV Conversions
π Learn More: I have compiled an “ultimate guide” on the Finxter blog that shows you the best method, respectively, to convert a CSV file to JSON, Excel, dictionary, Parquet, list, list of lists, list of tuples, text file, DataFrame, XML, NumPy array, and list of dictionaries.