• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Read CSV Into a List of Lists in Python

Read CSV Into a List of Lists in Python

Author: Aditya Raj
Last Updated: June 4, 2022

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!

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us