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

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

In this article, we will demonstrate how we can import a CSV into a list, list of lists or a list of tuples in python. We will be using pandas module for importing CSV contents to the list without headers.

Example Dataset :

CSV File name – data.csv

Id,Name,Course,City,Session
21,Jill,DSA,Texas,Night
22,Rachel,DSA,Tokyo,Day
23,Kirti,ML,Paris,Day
32,Veena,DSA,New York,Night

Read a CSV into list of lists in python :

1. Importing csv to a list of lists using csv.reader :

CSV.reader is a python built-in function from the CSV module which will help us read the CSV file into the python. Then passing the reader object into the list() will return a list of lists.

Let’s see the implementation of it.

#Program :

from csv import reader

#Opening the csv file as a list of lists in read mode
with open('data.csv', 'r') as csvObj:
 #The object having the file is passed into the reader
 csv_reader = reader(csvObj)
 #The reader object is passed into the list( ) to generate a list of lists
 rowList = list(csv_reader)
 print(rowList)
Output :
[['Id', 'Name', 'Course', 'City', 'Session'], 
['21', 'Jill', 'DSA', 'Texas', 'Night'], 
['22', 'Rachel', 'DSA', 'Tokyo', 'Day'], 
['23', 'Kirti', 'ML', 'Paris', 'Day'], 
['32', 'Veena', 'DSA', 'New York', 'Night']]

2. Selecting specific value in csv by specific row and column number :

 We can also select particular rows and columns from the CSV file by using Pandas. We have to read the CSV into a dataframe excluding the header and create a list of lists.

Let’s see the implementation of it.

#Program :

import pandas as pd

# Create a dataframe from the csv file
dfObj = pd.read_csv('data.csv', delimiter=',')
# User list comprehension 
# for creating a list of lists from Dataframe rows
rowList = [list(row) for row in dfObj.values]
# Print the list of lists i.e. only rows without the header
print(rowList)
Output :
[[21, 'Jill', 'DSA', 'Texas', 'Night'], 
[22, 'Rachel', 'DSA', 'Tokyo', 'Day'], 
[23, 'Kirti', 'ML', 'Paris', 'Day'], 
[32, 'Veena', 'DSA', 'New York', 'Night']]

3. Using Pandas to read csv into a list of lists with header :

To include the header row, we can first read the other rows like the previous example and then add the header to the list.

Let’s see the implementation of it.

#Program :

import pandas as pd

# Create a dataframe from the csv file
dfObj = pd.read_csv('data.csv', delimiter=',')
# User list comprehension 
# for creating a list of lists from Dataframe rows
rowList = [list(row) for row in dfObj.values]
#Adding the header
rowList.insert(0, dfObj.columns.to_list())
# Print the list of lists with the header
print(rowList)
Output :
[['Id', 'Name', 'Course', 'City', 'Session'], 
[21, 'Jill', 'DSA', 'Texas', 'Night'], 
[22, 'Rachel', 'DSA', 'Tokyo', 'Day'], 
[23, 'Kirti', 'ML', 'Paris', 'Day'], 
[32, 'Veena', 'DSA', 'New York', 'Night']]

Reading csv into list of tuples using Python :

Let’s add the contents of CSV file as a list of tuples. Each tuple will be representing a row and each value in the tuple represents a column value. Just like the way we added the contents into a list of lists from CSV, we will read the CSV file and then pass it into list function to create a list of tuples. The only difference here is the map( ) function that accepts function and input list arguments.

Let’s see the implementation of it.

#Program :

from csv import reader
# open file in read mode
with open('data.csv', 'r') as readerObj:
    # here passing the file object to reader() to get the reader object
    csv_reader = reader(readerObj)
    #Read all CSV files into the tuples
    tuplesList = list(map(tuple, csv_reader))
    # display the list of tuples
    print(tuplesList)
Output :

[('Id', 'Name', 'Course', 'City', 'Session'), ('21', 'Jill', 'DSA', 'Texas', 'Night'), ('22', 'Rachel', 'DSA', 'Tokyo', 'Day'), ('23', 'Kirti', 'ML', 'Paris', 'Day'), ('32', 'Veena', 'DSA', 'New York', 'Night')]

Reading csv into list of tuples using pandas & list comprehension :

We can load the contents of a CSV file into a dataframe by using read_csv( ) . Then using list comprehension we can convert the 2D numpy array into a list of tuples.

Let’s see the implementation of it.

#Program :

import pandas as pd
# Create a dataframe object from the csv file
dfObj = pd.read_csv('data.csv', delimiter=',')
# Create a list of tuples for Dataframe rows using list comprehension
tuplesList = [tuple(row) for row in dfObj.values]
# Print the list of tuple
print(tuplesList)
Output :
[(21, 'Jill', 'DSA', 'Texas', 'Night'), (22, 'Rachel', 'DSA', 'Tokyo', 'Day'), (23, 'Kirti', 'ML', 'Paris', 'Day'), (32, 'Veena', 'DSA', 'New York', 'Night')]

Reading csv into list of dictionaries using python :

We can also read the contents of a CSV file into dictionaries in python where each dictionary in the list will be a row from the CSV file. The CSV file contents are opened in read mode then they are passed into the Dict_reader( ) as a reader object, then it is passed into the list.

Let’s see the implementation of it.

#Program :

from csv import DictReader
# open file in read mode
with open('data.csv', 'r') as readerObj:
    # pass the reader file object to DictReader() to get the DictReader object
    dict_reader = DictReader(readerObj)
    # get a list of dictionaries from dct_reader
    dictList = list(dict_reader)
    # print the list of dict
    print(dictList)
Output :

[OrderedDict([('Id', '21'), ('Name', 'Jill'), ('Course', 'DSA'), ('City', 'Texas'), ('Session', 'Night')]), OrderedDict([('Id', '22'), ('Name', 'Rachel'), ('Course', 'DSA'), ('City', 'Tokyo'), ('Session', 'Day')]), OrderedDict([('Id', '23'), ('Name', 'Kirti'), ('Course', 'ML'), ('City', 'Paris'), ('Session', 'Day')]), OrderedDict([('Id', '32'), ('Name', 'Veena'), ('Course', 'DSA'), ('City', 'New York'), ('Session', 'Night')])]