Create a Dictionary of Lists in Python

I started working with Python more than a decade ago, and one of the most useful data structures I discovered was the dictionary of lists.

At first, I used Python dictionaries only for simple key-value pairs. But as my projects grew, I needed a way to store multiple values under a single key. That’s when the dictionary of lists became my go-to solution.

In this tutorial, I’ll walk you through everything I’ve learned about using a Python dictionary of lists. I’ll share multiple methods, real-world examples, and practical tips that I use in my day-to-day work.

What is a Python Dictionary of Lists?

A Python dictionary of lists is simply a dictionary where each key maps to a list instead of a single value. This allows us to store multiple related values under one key, making it perfect for grouping data.

For example, imagine we want to store U.S. states and their major cities. Instead of creating multiple variables, we can use a dictionary of lists.

Example: Basic Python Dictionary of Lists

Here’s a simple example:

# Creating a dictionary of lists
us_states = {
    "California": ["Los Angeles", "San Francisco", "San Diego"],
    "Texas": ["Houston", "Dallas", "Austin"],
    "New York": ["New York City", "Buffalo", "Rochester"]
}

# Accessing the list of cities in California
print("Cities in California:", us_states["California"])

This code makes it easy to group U.S. cities by state. I often use this approach when working with datasets that require grouping and categorization.

Method 1 – Create a Dictionary of Lists Manually

The first method is the easiest: manually assign lists to dictionary keys. This works well when you already know the data you want to store.

# Dictionary of lists for sports teams
sports_teams = {
    "Basketball": ["Lakers", "Warriors", "Celtics"],
    "Baseball": ["Yankees", "Dodgers", "Red Sox"],
    "Football": ["Patriots", "Cowboys", "Packers"]
}

print(sports_teams["Football"])

I executed the above example code and added the screenshot below.

python dictionary of lists

I use this method when working on small projects or when the data is fixed.

Method 2 – Use setdefault()

Sometimes we don’t know the keys ahead of time. In such cases, setdefault() is very handy. It initializes a list for a key if it doesn’t already exist.

# Using setdefault to build a dictionary of lists
students = {}
students.setdefault("Grade 9", []).append("Alice")
students.setdefault("Grade 9", []).append("Bob")
students.setdefault("Grade 10", []).append("Charlie")

print(students)

I executed the above example code and added the screenshot below.

dictionary of lists python

This method saves me from writing extra condition checks before adding values.

Method 3 – Use defaultdict from collections

One of my favorite Python tricks is using defaultdict. It automatically creates a list when a new key is accessed.

from collections import defaultdict

# defaultdict for dictionary of lists
grades = defaultdict(list)
grades["Grade 9"].append("David")
grades["Grade 9"].append("Emma")
grades["Grade 10"].append("Frank")

print(dict(grades))

I executed the above example code and added the screenshot below.

python dictionary list

This is my go-to method when handling large datasets, especially in data analysis projects.

Method 4 – Convert Two Lists into a Dictionary of Lists

Sometimes we have two separate lists and want to combine them into a dictionary of lists.

Here’s how I do it:

# Two lists
states = ["California", "Texas", "New York"]
cities = [
    ["Los Angeles", "San Diego"],
    ["Houston", "Austin"],
    ["New York City", "Buffalo"]
]

# Converting to dictionary of lists
state_cities = dict(zip(states, cities))
print(state_cities)

I executed the above example code and added the screenshot below.

dictionary list python

This method is useful when importing data from external sources like CSV or Excel files.

Method 5 – Update Lists Inside a Dictionary

Once you have a dictionary of lists, you’ll often need to update it.

Here’s an example of adding new values:

# Updating dictionary of lists
us_food = {
    "Fast Food": ["Burgers", "Fries"],
    "Desserts": ["Ice Cream", "Cookies"]
}

# Adding more items
us_food["Fast Food"].append("Hot Dogs")
us_food["Desserts"].append("Cheesecake")

print(us_food)

I use this pattern frequently when building recommendation systems or categorizing items dynamically.

Real-World Example – Python Dictionary of Lists for Student Grades

Let’s look at a more practical example from my experience. Suppose we want to track students’ grades across different subjects.

# Dictionary of lists for student grades
student_grades = {
    "Alice": [85, 90, 88],
    "Bob": [78, 82, 80],
    "Charlie": [92, 95, 91]
}

# Calculate average grade for each student
for student, grades in student_grades.items():
    avg = sum(grades) / len(grades)
    print(f"{student}'s average grade: {avg:.2f}")

This approach is perfect for handling structured data in schools, universities, or training programs.

Method 6 – Use Dictionary Comprehension

Python also allows us to create a dictionary of lists using dictionary comprehension.

# Dictionary comprehension example
numbers = {x: [x, x**2, x**3] for x in range(1, 6)}
print(numbers)

This is a concise way to generate dictionaries of lists when working with numeric or programmatically generated data.

Method 7 – Converting Dictionary Values to Lists

Sometimes, we need to convert dictionary values into lists explicitly.

# Converting dict values to lists
data = {"A": (1, 2), "B": (3, 4), "C": (5, 6)}
converted = {k: list(v) for k, v in data.items()}
print(converted)

I often use this when cleaning or transforming datasets for further processing.

Best Practices for Python Dictionary of Lists

Over the years, I’ve learned some best practices:

  • Use defaultdict for dynamic data insertion.
  • Keep keys descriptive (e.g., “California” instead of “CA”).
  • Avoid deeply nested dictionaries unless necessary.
  • Use dictionary comprehensions for generating structured data quickly.

These tips have saved me time and made my code more maintainable.

Conclusion

Working with a Python dictionary of lists has made my projects more organized and efficient.

Whether I’m grouping U.S. cities, managing student grades, or categorizing datasets, this data structure always comes in handy.

Both beginners and experienced developers can benefit from mastering this concept.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.