We often need to process csv files to analyze data related to a business problem. In this article, we will discuss how we can read a csv file into a list of lists in python.
Read CSV Into a List of Lists Using CSV.reader()
Python provides us with the csv module to work with csv files in python. To Access data from a csv
file, we often use a reader object created with the help of the csv.reader()
method.
After creating a reader object, we can read the csv file into a list of lists. For this, we will first open the csv file using the open()
function in the read mode. The open()
function takes the filename of the csv file as its first input argument and the literal “r
” as its second input argument to denote that the file will be opened in the read-only mode. After execution, the open()
method returns a file object that refers to the csv file.
Now, we will pass the file object to the reader()
method to create a reader object. The reader object is actually an iterator that contains each row of the csv file as a list. We can access each row of the csv file using a for loop and will read it into a list of lists as follows.
import csv
myFile = open('Demo.csv', 'r')
reader = csv.reader(myFile)
myList = []
for record in reader:
myList.append(record)
print("The list of lists is:")
print(myList)
Output:
The list of lists is:
[['Roll', 'Name', 'Language'], ['1', 'Aditya', 'Python'], ['2', 'Sam', ' Java'], ['3', ' Chris', ' C++']]
If you want to skip the header of the csv file, you can do it using the next()
function. The next()
function, when executed on an iterator, returns an element from the iterator and moves the iterator to the next element. Outside the for loop, you can use the next()
function once to read the csv file into a list without the header as follows.
import csv
myFile = open('Demo.csv', 'r')
reader = csv.reader(myFile)
print("The header is:")
print(next(reader))
myList = []
for record in reader:
myList.append(record)
print("The list of lists is:")
print(myList)
Output:
The header is:
['Roll', 'Name', 'Language']
The list of lists is:
[['1', 'Aditya', 'Python'], ['2', 'Sam', ' Java'], ['3', ' Chris', ' C++']]
Instead of using the for loop to read the csv from the reader object , you can use the list()
constructor. Here, we will pass the reader object to the list()
constructor and it will return a list of lists as shown below.
import csv
myFile = open('Demo.csv', 'r')
reader = csv.reader(myFile)
myList = list(reader)
print("The list of lists is:")
print(myList)
Output:
The list of lists is:
[['Roll', 'Name', 'Language'], ['1', 'Aditya', 'Python'], ['2', 'Sam', ' Java'], ['3', ' Chris', ' C++']]
Similarly, you can use the list()
constructor along with the next()
method and the csv.reader()
method to read a csv file without its header as follows.
import csv
myFile = open('Demo.csv', 'r')
reader = csv.reader(myFile)
print("The header is:")
print(next(reader))
myList = list(reader)
print("The list of lists is:")
print(myList)
Output:
The header is:
['Roll', 'Name', 'Language']
The list of lists is:
[['1', 'Aditya', 'Python'], ['2', 'Sam', ' Java'], ['3', ' Chris', ' C++']]
Conclusion
In this article, we have discussed different approaches to read a csv file into a list of lists in python. To know more about lists, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
I hope you enjoyed reading this article.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.