Iterate Through a Python Dictionary with Multiple Values

I first started using Python more than a decade ago, and dictionaries quickly became one of my favorite data structures. They are flexible, fast, and perfect for storing key-value pairs. But sometimes, I needed each key in a dictionary to hold multiple values instead of just one.

That’s when I discovered that Python makes it simple to store lists, sets, or tuples as values inside a dictionary. The challenge, however, is iterating through these values efficiently.

In this tutorial, I’ll walk you through different methods I personally use to iterate through a Python dictionary with multiple values.

Use a Dictionary with Multiple Values in Python

In real-world projects, it’s common to need multiple values for a single key. For example, imagine you’re working with customer data in the USA. A customer might have multiple phone numbers, multiple addresses, or multiple orders.

Instead of creating separate keys for each detail, you can store all related values under one key. This keeps your data organized and makes it easier to process later.

Create a Python Dictionary with Multiple Values

The most common way to create a dictionary with multiple values is to use a list as the value.

Here’s a simple example:

# Dictionary with multiple values (lists)
customers = {
    "John": ["New York", "Chicago", "Miami"],
    "Sarah": ["Los Angeles", "San Francisco"],
    "Mike": ["Dallas", "Houston"]
}

print(customers)

In this example, each customer name is a key, and the value is a list of cities where they have offices. This structure is easy to build and very flexible for iteration.

Method 1 – Iterate Using a For Loop

The simplest way to iterate through a Python dictionary with multiple values is by using a for loop.

Here’s how I usually do it:

# Iterate through dictionary with multiple values
customers = {
    "John": ["New York", "Chicago", "Miami"],
    "Sarah": ["Los Angeles", "San Francisco"],
    "Mike": ["Dallas", "Houston"]
}

for name, cities in customers.items():
    print(f"{name} has offices in:")
    for city in cities:
        print(f" - {city}")

You can see the output in the screenshot below.

iterate through dictionary python

This method loops through each key-value pair. Then, I use another loop to go through the list of values. It’s simple, readable, and works perfectly for most cases.

Method 2 – Iterate with Dictionary Comprehension

Sometimes, I want to create a new dictionary while iterating. In Python, dictionary comprehensions are a neat way to do this.

# Dictionary comprehension to count values
customers = {
    "John": ["New York", "Chicago", "Miami"],
    "Sarah": ["Los Angeles", "San Francisco"],
    "Mike": ["Dallas", "Houston"]
}

city_count = {name: len(cities) for name, cities in customers.items()}
print(city_count)

You can see the output in the screenshot below.

python iterate dictionary

This code creates a new dictionary that stores the number of cities each customer has. It’s concise and Pythonic, making it a great option when you need quick transformations.

Method 3 – Iterate Through Dictionary with Index

There are times when I want to access values by their index while iterating. In that case, I use the enumerate() function in Python.

# Iterate with index
customers = {
    "John": ["New York", "Chicago", "Miami"],
    "Sarah": ["Los Angeles", "San Francisco"],
    "Mike": ["Dallas", "Houston"]
}

for name, cities in customers.items():
    print(f"{name} has offices in:")
    for index, city in enumerate(cities, start=1):
        print(f"{index}. {city}")

You can see the output in the screenshot below.

iterating through dictionary python

This gives me a numbered list of cities for each customer. It’s especially useful when you’re displaying data to users or generating reports.

Method 4 – Use itertools for Advanced Iteration

For more advanced use cases, I sometimes rely on the itertools module in Python. This is helpful when I want to flatten all values across the dictionary.

import itertools

customers = {
    "John": ["New York", "Chicago", "Miami"],
    "Sarah": ["Los Angeles", "San Francisco"],
    "Mike": ["Dallas", "Houston"]
}

all_cities = list(itertools.chain.from_iterable(customers.values()))
print(all_cities)

You can see the output in the screenshot below.

iterate dictionary python

This method combines all the lists into one flat list. It’s a powerful way to work with large datasets where you need to process all values at once.

Method 5 – Iterate Through Nested Dictionaries

Sometimes, instead of lists, I store dictionaries as values inside another dictionary. This is common when working with structured data like orders, invoices, or user profiles.

# Nested dictionary
customers = {
    "John": {"age": 35, "cities": ["New York", "Chicago"]},
    "Sarah": {"age": 29, "cities": ["Los Angeles", "San Francisco"]},
    "Mike": {"age": 42, "cities": ["Dallas", "Houston"]}
}

for name, info in customers.items():
    print(f"{name} is {info['age']} years old and has offices in:")
    for city in info["cities"]:
        print(f" - {city}")

This approach lets me keep multiple attributes neatly organized under each key. It’s slightly more complex but very powerful for real-world applications.

Best Practices When Iterating Through Python Dictionaries with Multiple Values

Over the years, I’ve learned a few best practices that make working with these dictionaries easier:

  • Use descriptive variable names like cities, orders, or addresses.
  • Stick to lists for ordered data, sets for unique values, and dictionaries for structured attributes.
  • When performance matters, avoid nested loops on very large datasets. Instead, use tools like itertools.
  • Always check if the key exists before iterating to avoid errors.

These small habits save time and reduce bugs in larger projects.

Real-World Example: Tracking Customer Orders

Let’s put everything together with a practical USA-based example.

Imagine you’re tracking customer orders for an online store. Each customer can have multiple orders.

# Real-world example: customer orders
orders = {
    "John": ["Laptop", "Smartphone", "Headphones"],
    "Sarah": ["Tablet", "Camera"],
    "Mike": ["Smartwatch", "Printer", "Monitor"]
}

for customer, items in orders.items():
    print(f"{customer} ordered:")
    for item in items:
        print(f" - {item}")

This simple structure makes it easy to manage multiple orders per customer. I’ve used this exact approach in projects where I had to process thousands of transactions.

Iterating through a Python dictionary with multiple values is a skill every developer should master. I’ve shown you several methods: from simple loops to comprehensions, from indexed iteration to advanced itertools, and even nested dictionaries.

Which method you use depends on your specific project, but the good news is that Python gives you all the flexibility you need.

You may also like to read other articles:

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.