Convert CSV to List of Tuples in Python

4.8/5 - (5 votes)
Convert CSV to List of Tuples in Python

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

Method 1: csv.reader()

Method 1: csv.reader()

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)

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 tuples using the following Python one-liner idea:

Open the file using open(), pass the file object into csv.reader(), and convert the CSV reader object to a list using the list() built-in function in Python with a generator expression to convert each inner list to a tuple.

Here’s how that looks:

import csv; lst=list(tuple(line) for line in 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 previously shown list lst:

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

The converted new_lst now contains a list of tuples 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 tuples 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(). Third, convert the nested list to a list of tuples and you’re done.

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

import pandas as pd

# CSV to DataFrame
df = pd.read_csv('my_file.csv', header=None)

# DataFrame to List of Lists
lst = df.values.tolist()

# List of Lists to List of Tuples:
new_lst = [tuple(x) for x in lst]

print(new_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:

# One-Liner to convert CSV to list of tuples:
lst = [tuple(x) for x in 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 to unnecessary risks and complexities.

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

Sure!

To read a CSV to a list of tuples in pure Python, open the file using open('my_file.csv'), read all lines into a variable using f.readlines(). Iterate over all lines, strip them from whitespace using strip(), split them on the delimiter ',' using split(','), and pass everything in the tuple() function.

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 = [tuple(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:

A Simple Introduction to List Comprehension in Python

🌍 Related Tutorial: Understanding List Comprehension in Python.

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.

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