Python: Read CSV into a list of lists or tuples or dictionaries | Import csv to list

In this article we will discuss how to import a CSV into list. This list can be a list of lists, list of tuples or list of dictionaries. We will also use pandas module and cover scenarios for importing CSV contents to list with or without headers.

Suppose we have a CSV file ‘students.csv’ and its contents are,

Id,Name,Course,City,Session
21,Mark,Python,London,Morning
22,John,Python,Tokyo,Evening
23,Sam,Python,Paris,Morning
32,Shaun,Java,Tokyo,Morning

Now let’s see how to import the contents of this csv file into a list.

Read a CSV into list of lists in python

There are different ways to load csv contents to a list of lists,

Import csv to a list of lists using csv.reader

Python has a built-in csv module, which provides a reader class to read the contents of a csv file. Let’s use that,

from csv import reader

# read csv file as a list of lists
with open('students.csv', 'r') as read_obj:
    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Pass reader object to list() to get a list of lists
    list_of_rows = list(csv_reader)
    print(list_of_rows)

Output:

[['Id', 'Name', 'Course', 'City', 'Session'],
 ['21', 'Mark', 'Python', 'London', 'Morning'],
 ['22', 'John', 'Python', 'Tokyo', 'Evening'],
 ['23', 'Sam', 'Python', 'Paris', 'Morning'],
 ['32', 'Shaun', 'Java', 'Tokyo', 'Morning']]

It created a list of lists containing all rows of csv file and print that list of lists.

How did it work ?

We opened the file in read mode and then passed the file object to csv.reader() function. It returned an iterator, which can be used to iterate over all the lines of csv file. But we passed this iterator object to list() function, which return a list of lists i.e. where each list represents a row of csv and each item in the list represents a cell / column in that row.

Select specific value in csv by specific row and column number

We can also select an individual element in csv file by row & column number using this list of lists created above. For example, from the list of lists i.e. list_of_rows (created above), let’s select the value from csv at row number 3 and column number 2,

# select the value from csv at row number 3 and column number 2
row_number = 3
col_number = 2
value = list_of_rows[row_number - 1][col_number - 1]

print('Value in cell at 3rd row and 2nd column : ', value)

Output:

Value in cell at 3rd row and 2nd column :  John

Use Pandas to read csv into a list of lists without header

In the previous example, we loaded all rows (including header) into a list of lists. Suppose we want to read all rows into a list of lists except header. We can achieve that easily using pandas,

import pandas as pd

# Create a dataframe from csv
df = pd.read_csv('students.csv', delimiter=',')

# User list comprehension to create a list of lists from Dataframe rows
list_of_rows = [list(row) for row in df.values]

# Print list of lists i.e. rows
print(list_of_rows)

Output:

[['21', 'Mark', 'Python', 'London', 'Morning'],
 ['22', 'John', 'Python', 'Tokyo', 'Evening'],
 ['23', 'Sam', 'Python', 'Paris', 'Morning'],
 ['32', 'Shaun', 'Java', 'Tokyo', 'Morning']]

It created a list of lists containing all rows of csv except header and print that list of lists.

How did it work ?

We loaded the csv to a Dataframe using read_csv() function. As Dataframe.values returns a 2D Numpy representation of all rows of Dataframe excluding header. So, we iterated over all rows of this 2D Numpy Array using list comprehension and created a list of lists. Where each list represents a row of csv and each item in the list represents a cell / column in that row.

Use Pandas to read csv into a list of lists with header

In above example, header of csv was skipped by default. So, if you want header too in this list of lists,
then we need to insert it in list separately in the end of the above example, like this,

import pandas as pd

# Create a dataframe from csv
df = pd.read_csv('students.csv', delimiter=',')

# User list comprehension to create a list of lists from Dataframe rows
list_of_rows = [list(row) for row in df.values]

# Insert Column names as first list in list of lists
list_of_rows.insert(0, df.columns.to_list())

# Print list of lists i.e. rows
print(list_of_rows)

Output:

[['Id', 'Name', 'Course', 'City', 'Session'],
 ['21', 'Mark', 'Python', 'London', 'Morning'],
 ['22', 'John', 'Python', 'Tokyo', 'Evening'],
 ['23', 'Sam', 'Python', 'Paris', 'Morning'],
 ['32', 'Shaun', 'Java', 'Tokyo', 'Morning']]

It created a list of lists containing all rows of csv including header and print that list of lists.

How did it work?

We loaded the csv to a Dataframe using read_csv() function. As Dataframe.values returns a 2D numpy representation of all rows of Dataframe excluding header. So, we iterated over all rows of this 2D Numpy Array using list comprehension and created a list of lists. Dataframe.columns gives column names, so we converted that to a list and inserted at the start of the rows list.

Read csv into list of tuples using Python

Let’s load all the contents of students.csv to a list of tuples, where each tuple in the list represents a row and each value in the tuple represents a cell / column value for that particular row.

from csv import reader

# open file in read mode
with open('students.csv', 'r') as read_obj:
    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Get all rows of csv from csv_reader object as list of tuples
    list_of_tuples = list(map(tuple, csv_reader))
    # display all rows of csv
    print(list_of_tuples)

Output

[('Id', 'Name', 'Course', 'City', 'Session'),
 ('21', 'Mark', 'Python', 'London', 'Morning'),
 ('22', 'John', 'Python', 'Tokyo', 'Evening'),
 ('23', 'Sam', 'Python', 'Paris', 'Morning'),
 ('32', 'Shaun', 'Java', 'Tokyo', 'Morning')]

How did it work?

We opened the csv file in read mode and then passed the file object to csv.reader() function.It returned an iterator csv_reader, with which we can iterate over all the rows of csv. But we passed it into the map() function as an argument along with tuple() function as callback i.e.,

mapped_object = map(tuple , csv_reader)

map() function accepts a function & input list as arguments. For each item in the input list it applies the function and stores the result in a mapped object.

So, in this case map() function, iterated over all the rows of csv using iterator csv_reader and applied the function tuple() to each item. Then stored the returned tuple i.e. a tuple for a row, to the mapped object. So, now our mapped_object contains tuples, i.e. one tuple for each row.

Then we converted this mapped object of tuples to a list of tuples i.e.

list_of_tuples = list(mapped_object)

So, this is how we imported a csv file to a list of tuples. We can do the same using pandas & list comprehension in a single line i.e.

Read csv into list of tuples using pandas & list comprehension

import pandas as pd

# Create a dataframe from csv
df = pd.read_csv('students.csv', delimiter=',')

# Create a list of tuples for Dataframe rows using list comprehension
list_of_tuples = [tuple(row) for row in df.values]

# Print list of tuple
print(list_of_tuples)

Output

[('21', 'Mark', 'Python', 'London', 'Morning'),
 ('22', 'John', 'Python', 'Tokyo', 'Evening'),
 ('23', 'Sam', 'Python', 'Paris', 'Morning'),
 ('32', 'Shaun', 'Java', 'Tokyo', 'Morning')]

We loaded the csv to a Dataframe using read_csv() function. Dataframe.values returned a Numpy representation of the DataFrame i.e. is a 2D numpy array. Using list comprehension we iterated over each row of this 2D array and for each row we converted that to a tuple, then append it to a list.
Finally list comprehension returned a list of tuples, where each tuple in the list represents a row and each value in the tuple represents a cell / column value for that particular row.

Read csv into list of dictionaries using python

from csv import DictReader

# open file in read mode
with open('students.csv', 'r') as read_obj:
    # pass the file object to DictReader() to get the DictReader object
    dict_reader = DictReader(read_obj)
    # get a list of dictionaries from dct_reader
    list_of_dict = list(dict_reader)
    # print list of dict i.e. rows
    print(list_of_dict)

Output:

[{'Id': '21', 'Name': 'Mark', 'Course': 'Python', 'City': 'London', 'Session': 'Morning'},
 {'Id': '22', 'Name': 'John', 'Course': 'Python', 'City': 'Tokyo', 'Session': 'Evening'},
 {'Id': '23', 'Name': 'Sam', 'Course': 'Python', 'City': 'Paris', 'Session': 'Morning'},
 {'Id': '32', 'Name': 'Shaun', 'Course': 'Java', 'City': 'Tokyo', 'Session': 'Morning'}]

We got a list of dictionaries, where each dictionary in the list represents a csv row and contains pairs of column names & column values for that row, as key / value pairs.

How did it work?

We opened the csv file in read mode and then passed the file object to csv.DictReader() function. It returned an iterator dict_reader, with which we can iterate over all the rows of csv and fetch each row content as a dictionary. But we passed this iterator to the list() function, which returned a list of dictionaries i.e. rows.

An important point is, whenever we pass an iterable item to a list constructor i.e. list(), then it internally iterates over all the elements of the data structure to which this iterator object is pointing
& stores them into a list. In the end returns that list.

The complete example is as follows,

from csv import reader
from csv import DictReader
import pandas as pd


def main():
    print('**** Read csv into a list of lists ****')

    # read csv file as a list of lists
    with open('students.csv', 'r') as read_obj:
        # pass the file object to reader() to get the reader object
        csv_reader = reader(read_obj)
        # Pass reader object to list() to get a list of lists
        list_of_rows = list(csv_reader)
        print(list_of_rows)

    print('*** Select value in csv Specific Row and Column ***')

    # select the value from csv at row number 3 and column number 2
    row_number = 3
    col_number = 2
    value = list_of_rows[row_number - 1][col_number - 1]

    print('Value in cell at 3rd row and 2nd column : ', value)

    print('*** Use pandas to read rows in a csv file to a list of list without header ***')

    # Create a dataframe from csv
    df = pd.read_csv('students.csv', delimiter=',')
    # User list comprehension to create a list of lists from Dataframe rows
    list_of_rows = [list(row) for row in df.values]
    # Print list of lists i.e. rows
    print(list_of_rows)

    print('*** Use pandas to read rows in a csv file to a list of list ***')

    # Create a dataframe from csv
    df = pd.read_csv('students.csv', delimiter=',')
    # User list comprehension to create a list of lists from Dataframe rows
    list_of_rows = [list(row) for row in df.values]
    # Insert Column names as first list in list of lists
    list_of_rows.insert(0, df.columns.to_list())
    # Print list of lists i.e. rows
    print(list_of_rows)

    print('**** Read csv into list of tuples using Python ****')

    # open file in read mode
    with open('students.csv', 'r') as read_obj:
        # pass the file object to reader() to get the reader object
        csv_reader = reader(read_obj)
        # Get all rows of csv from csv_reader object as list of tuples
        list_of_tuples = list(map(tuple, csv_reader))
        # display all rows of csv
        print(list_of_tuples)

    print('*** Read csv into list of tuples using pandas in python (without header) ***')

    # Create a dataframe from csv
    df = pd.read_csv('students.csv', delimiter=',')

    # Create a list of tuples for Dataframe rows using list comprehension
    list_of_tuples = [tuple(row) for row in df.values]
    # Print list of tuple
    print(list_of_tuples)

    print('**** Read csv into list of dictionaries using python ****')

    # open file in read mode
    with open('students.csv', 'r') as read_obj:
        # pass the file object to DictReader() to get the DictReader object
        dict_reader = DictReader(read_obj)
        # get a list of dictionaries from dct_reader
        list_of_dict = list(dict_reader)
        # print list of dict i.e. rows
        print(list_of_dict)


if __name__ == '__main__':
    main()

Output:

**** Read csv into a list of lists ****
[['Id', 'Name', 'Course', 'City', 'Session'], ['21', 'Mark', 'Python', 'London', 'Morning'], ['22', 'John', 'Python', 'Tokyo', 'Evening'], ['23', 'Sam', 'Python', 'Paris', 'Morning'], ['32', 'Shaun', 'Java', 'Tokyo', 'Morning']]
*** Select value in csv Specific Row and Column ***
Value in cell at 3rd row and 2nd column :  John
*** Use pandas to read rows in a csv file to a list of list without header ***
[[21, 'Mark', 'Python', 'London', 'Morning'], [22, 'John', 'Python', 'Tokyo', 'Evening'], [23, 'Sam', 'Python', 'Paris', 'Morning'], [32, 'Shaun', 'Java', 'Tokyo', 'Morning']]
*** Use pandas to read rows in a csv file to a list of list ***
[['Id', 'Name', 'Course', 'City', 'Session'], [21, 'Mark', 'Python', 'London', 'Morning'], [22, 'John', 'Python', 'Tokyo', 'Evening'], [23, 'Sam', 'Python', 'Paris', 'Morning'], [32, 'Shaun', 'Java', 'Tokyo', 'Morning']]
**** Read csv into list of tuples using Python ****
[('Id', 'Name', 'Course', 'City', 'Session'), ('21', 'Mark', 'Python', 'London', 'Morning'), ('22', 'John', 'Python', 'Tokyo', 'Evening'), ('23', 'Sam', 'Python', 'Paris', 'Morning'), ('32', 'Shaun', 'Java', 'Tokyo', 'Morning')]
*** Read csv into list of tuples using pandas in python (without header) ***
[(21, 'Mark', 'Python', 'London', 'Morning'), (22, 'John', 'Python', 'Tokyo', 'Evening'), (23, 'Sam', 'Python', 'Paris', 'Morning'), (32, 'Shaun', 'Java', 'Tokyo', 'Morning')]
**** Read csv into list of dictionaries using python ****
[{'Id': '21', 'Name': 'Mark', 'Course': 'Python', 'City': 'London', 'Session': 'Morning'}, {'Id': '22', 'Name': 'John', 'Course': 'Python', 'City': 'Tokyo', 'Session': 'Evening'}, {'Id': '23', 'Name': 'Sam', 'Course': 'Python', 'City': 'Paris', 'Session': 'Morning'}, {'Id': '32', 'Name': 'Shaun', 'Course': 'Java', 'City': 'Tokyo', 'Session': 'Morning'}]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top