Python – Convert CSV to List of Lists

4.7/5 - (3 votes)

Problem Formulation

Python - Convert CSV to List of Lists

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]]

Method 1: csv.reader()

Method 1: csv.reader()

To convert a CSV file 'my_file.csv' into a list of lists in Python, use the csv.reader(file_obj) method to create a CSV file reader. Then convert the resulting object to a list using the list() constructor.

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(reader)

Output:

print(lst)
# [['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]

Method 2: One-Liner

Method 2: One-Liner

You can also convert a CSV to a list of lists using the following Python one-liner that opens the file using open(), passes the file object into the csv.reader() function, and convert the CSV reader object to a list using the list() built-in function in Python.

Here’s how that looks:

import csv; lst=list(csv.reader(open('my_file.csv'))); print(lst)

Concise, isn’t it? 🙂

The output is the same:

[['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]

By the way: Do you need all inner elements converted to a certain data type such as integer?

If so, use the following approach on top of the nested list of strings, lst, obtained from the previous methods:

new_lst = [[int(x) for x in inner] for inner in lst]

The converted new_lst now contains a nested list of ints:

print(new_lst)
# [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

Method 3: Pandas

Method 3: Pandas

You can convert a CSV to a list of lists with Pandas by first reading the CSV without header line using pd.read_csv('my_file.csv', header=None) function and second converting the resulting DataFrame to a nested list using df.values.tolist().

Here’s an example that converts the CSV to a Pandas DataFrame and then to a nested raw Python list:

import pandas as pd

df = pd.read_csv('my_file.csv', header=None)
lst = df.values.tolist()

print(lst)
# [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

This was easy, wasn’t it? 🙂

Of course, you can also one-linerize it by chaining commands like so:

lst = pd.read_csv('my_file.csv', header=None).values.tolist()

Method 4: Raw Python No Dependency

Method 4: Raw Python No Dependency

If you’re like me, you try to avoid using dependencies if they are not needed. Raw Python is often more efficient and simple enough anyways. Also, you don’t open yourself up against unnecessary risks and complexities.

Question: So, is there a simple way to read a CSV to a list of lists in raw Python without external dependencies?

Sure!

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]
    print(lst)

Feel free to check out my detailed video in case you need a refresher on the powerful Python concept list comprehension:

🌍 Related Tutorial: Understanding List Comprehension in Python.

Become a One-Liner Wizard!

In case you enjoyed the one-liners presented here and you want to improve your Python skills, feel free to get yourself a copy of my best-selling Python book:

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!