The 2 Best ways to read CSV files into a List of Dictionaries with Python

What will we cover?

First we will look at the classical way to a read CSV file into Python and understand why we want to read it into a list of Dictionaries.

Then we will demonstrate the two most convenient ways to read CSV files into a list of dictionaries in Python.

See this more general introduction to CSV files.

The classical way to read CSV files in Python

To make the demonstration we need a CSV file.

Save the following content in NameRecords.csv

First name,Last name,Age
Connar,Ward,15
Rose,Peterson,18
Paul,Cox,12
Hanna,Hicks,10

Then we will read the content with the default CSV reader in Python.

import csv

with open('NameRecords.csv') as csvfile:
    csv_reader = csv.reader(csvfile)
    rows = list(csv_reader)

Then if you want to get the content it will be in a list of list represented in rows.

for row in rows:
    print(row)

Resulting in the following output.

['First name', 'Last name', 'Age']
['Connar', 'Ward', '15']
['Rose', 'Peterson', '18']
['Paul', 'Cox', '12']
['Hanna', 'Hicks', '10']

Which illustrates two problems.

  1. The column names are given in the first item of the list.
  2. The row is a list of the items, and you need to keep track of what they represent.

Hence, if we could read the content directly into a list of dictionaries, it would make your life as a programmer easier.

What do I mean? See the two methods below.

Method 1: Using DictReader

This is possible the classical way to do it and uses standard Python library CSV.

First you need a CSV file to work with. Save the following content in NameRecords.csv

First name,Last name,Age
Connar,Ward,15
Rose,Peterson,18
Paul,Cox,12
Hanna,Hicks,10

Then the following will read the content into a list of dictionaries.

import csv

with open("files/NameRecords.csv", "r") as f:
    csv_reader = csv.DictReader(f)
    name_records = list(csv_reader)

Access to the content can be achieved as follows.

print(name_records[0])
print(name_records[0]['First name'])
  • Advantage of approach: No additional library needs installation.
  • Disadvantage of approach: You cannot access remote files.

Method 2: Using pandas read_csv()

This approach can read remote data.

import pandas as pd

url = "https://raw.githubusercontent.com/LearnPythonWithRune/LearnPython/main/files/NameRecords.csv"
name_records = pd.read_csv(url)

name_records = name_records.to_dict('records')

Again the data is structured in a list of records in dictionaries.

  • Advantage of approach: Can read remote files directly (like from GitHub)
  • Disadvantage of approach: Need to install pandas library.

For an example see this project with pandas read_csv.

Want to learn more?

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Leave a Reply