• 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 List of Dictionaries in Python

Read CSV Into List of Dictionaries in Python

Author: Aditya Raj
Last Updated: July 10, 2022

CSV files are used to store structured data where each row in the csv file stores an entry in which each value is associated with the column name. Similarly, a python dictionary is used to store key-value pairs in Python. In this article, we will discuss how we can read a csv file into a list of dictionaries in python.

Read CSV Into List of Dictionaries Using csv.DictReader()

In python, we can use the csv module to work with csv files. To read a csv file into a list of dictionaries, we will create a csv.DictReader object using the csv.DictReader() method. After creating the DictReader object, we can create a list of dictionaries from the csv file using the following steps.

  • First, we will open the csv file using the open() function in the read mode. The open() function takes the file name as its first input argument and the literal “r” as its second input argument to show that the file is opened in the read mode. After execution, it returns a file object that contains the csv file.
  • After obtaining the file object from the open() function, we will create a DictReader object using the csv.DictReader() function. The csv.DictReader() function takes the file object as its input argument and returns a DictReader object.
  • The DictReader object works as an iterator and contains each row of the csv file as a dictionary. In the dictionary, the keys consist of the column names of the csv file whereas the values associated with the keys are values present in a particular column in a row.
  • To read the csv file into list of dictionaries, we will first create an empty list. After that, we will we will add each dictionary from the DictReader object to the list using a for loop. After execution of the for loop, we will get the entire csv file as a list of dictionaries.
  • Don’t forget to close the file using the close() method at the end of the program.

The program to read a csv into list of dictionaries using the DictReader() method and a for loop is as follows.

import csv

myFile = open('Demo.csv', 'r')
reader = csv.DictReader(myFile)
myList = list()
for dictionary in reader:
    myList.append(dictionary)
print("The list of dictionaries is:")
print(myList)

Output:

The list of dictionaries is:
[{'Roll': '1', 'Name': 'Aditya', 'Language': 'Python'}, {'Roll': '2', 'Name': 'Sam', 'Language': ' Java'}, {'Roll': '3', 'Name': ' Chris', 'Language': ' C++'}]

Instead of using the for loop, you can also use the list() constructor to convert the DictReader object into a list as follows.

import csv

myFile = open('Demo.csv', 'r')
reader = csv.DictReader(myFile)
myList = list(reader)
print("The list of dictionaries is:")
print(myList)

Output:

The list of dictionaries is:
[{'Roll': '1', 'Name': 'Aditya', 'Language': 'Python'}, {'Roll': '2', 'Name': 'Sam', 'Language': ' Java'}, {'Roll': '3', 'Name': ' Chris', 'Language': ' C++'}]

Conclusion

In this article, we have discussed how we can read a csv file into a list of dictionaries in python. To learn more about lists, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.

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