Skip to main content

How to Create Dictionaries from CSV Data in Python

The CSV (Comma-Separated Values) format is a standard for storing tabular data. While Python can read CSVs as lists of strings, converting them into dictionaries makes the data significantly easier to work with. Using a dictionary allows you to access data by column name (e.g., row['Price']) rather than by integer index (e.g., row[2]), improving code readability and reducing errors if column orders change.

This guide explains how to use the built-in csv module to parse CSV files directly into Python dictionaries.

Prerequisites: Creating a Sample CSV

To follow along, let's first create a simple CSV file named employees.csv. You can create this manually or run the Python script below to generate it.

import csv

data = [
['Name', 'Department', 'Salary'],
['Alice', 'Engineering', '90000'],
['Bob', 'HR', '60000'],
['Charlie', 'Engineering', '95000']
]

# Write sample data to 'employees.csv'
with open('employees.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)

print("Sample file 'employees.csv' created.")

Output:

Sample file 'employees.csv' created.

Method 1: Using csv.DictReader (Standard Approach)

The csv module provides a specific class called DictReader. It automatically reads the first row of the CSV file as the header (keys) and maps subsequent rows to these keys.

Reading Data Row by Row

import csv

# ✅ Correct: Using DictReader to map headers to values
with open('employees.csv', 'r') as file:
reader = csv.DictReader(file)

# Iterate over the reader object
for row in reader:
# Each row is an OrderedDict (or dict in Python 3.8+)
print(f"{row['Name']} works in {row['Department']}")

Output:

Alice works in Engineering
Bob works in HR
Charlie works in Engineering
note

csv.DictReader does not return a list immediately; it returns an iterator. This is memory-efficient for large files. If you need a list of dictionaries, use list(reader).

Method 2: Handling CSVs Without Headers

Sometimes, CSV files contain only data without a header row. If you use DictReader directly, it will incorrectly treat the first row of data as the headers. You must specify the fieldnames parameter manually.

First, let's create a headerless file:

# Create a headerless CSV
data_no_header = [['David', 'Marketing'], ['Eve', 'Sales']]
with open('no_header.csv', 'w', newline='') as f:
csv.writer(f).writerows(data_no_header)

Specifying Fieldnames

import csv

# ⛔️ Incorrect: Without fieldnames, 'David' becomes a key, 'Marketing' becomes a value
# with open('no_header.csv', 'r') as file:
# reader = csv.DictReader(file) ...

# ✅ Correct: Manually provide keys
with open('no_header.csv', 'r') as file:
# We define the keys list
cols = ['Name', 'Dept']
reader = csv.DictReader(file, fieldnames=cols)

for row in reader:
print(dict(row))

Output:

{'Name': 'David', 'Dept': 'Marketing'}
{'Name': 'Eve', 'Dept': 'Sales'}

Method 3: Filtering and Analyzing Data

Once your data is in a list of dictionaries, you can easily filter, sort, or aggregate it using standard Python list comprehensions.

import csv

# Load data into a list
with open('employees.csv', 'r') as file:
reader = csv.DictReader(file)
employees = list(reader)

# Filter: Find employees in Engineering
engineers = [e for e in employees if e['Department'] == 'Engineering']

# Aggregation: Calculate total salary for Engineers
# Note: CSV data is always strings, convert to int/float for math
total_eng_salary = sum(int(e['Salary']) for e in engineers)

print(f"Engineers found: {len(engineers)}")
print(f"Total Engineering Salary: ${total_eng_salary}")

Output:

Engineers found: 2
Total Engineering Salary: $185000
tip

Always handle type conversion (like int() or float()) manually when processing CSV data, as csv reads everything as strings.

Conclusion

Converting CSV data to dictionaries is a powerful way to write clean, readable data processing scripts.

  1. Use csv.DictReader when your file has headers; it maps columns automatically.
  2. Use the fieldnames parameter if your file lacks headers.
  3. Convert to a list (list(reader)) if you need to filter or sort the data in memory.
  4. Cast types manually, as all CSV values are read as strings.