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

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:

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

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]['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 = ""
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.

Python Like a Pro?

If you’re serious about learning Python, there’s nothing better than strong commits. At your request, we have created an improved version of this popular free online course.

This version has the following benefits to enhance your learning journey.

  1. Tracking your progress in the course.
  2. Questionaries to ensure you understand concepts between important lessons.
  3. Downloadable Cheat Sheets for fast lookup what you learned.
  4. Direct Q&A with the instructor to help you to understand the material better.
  5. Added material for better explanations and insider knowledge.
  6. Extra videos with more explanations and stories.
  7. Certificate at completion.

Start the change in your life and commit to doing something amazing that you have always dreamed of.

Sign up and become part of the exclusive Python Like a Pro elite.


Published by

Recent Posts

Learn Python FREE Online

Why learn Python? There are many reasons to learn Python, and that is the power…

3 days ago

How to Check if a Number is Even or Odd with Python

What will you learn? How to use the modulo operator to check if a number…

1 week ago

The Truth About Being a Python Software Contractor

There are a lot of Myths out there There are lot of Myths about being…

2 months ago

Do This and 10X Your Salary as a Software Engineer

To be honest, I am not really a great programmer - that is not what…

2 months ago

Ultimate Guide to the Data Science Career Path

What does it take to become a Data Scientist? Data Science is in a cross…

2 months ago

How to Setup a MySQL Server in Docker for Your Python Project

What will you learn? Need to setup a SQL server? You don’t need to install…

4 months ago