Python Dictionary Comprehension

When I first started working with Python dictionaries more than a decade ago, I often found myself writing long for loops just to build or transform them.

Over time, I discovered dictionary comprehension, a powerful and simple way to create dictionaries in just one line of code. It not only saves time but also makes your code cleaner and easier to read.

In this tutorial, I’ll walk you through different ways I use dictionary comprehension in real projects. Each example is practical, simple, and something you can start using right away.

What is Dictionary Comprehension in Python?

Dictionary comprehension is a shorthand way to create or manipulate dictionaries.
It works just like list comprehension, but returns a dictionary instead of a list.

The basic syntax looks like this:

{key: value for item in iterable}

This lets you generate key-value pairs dynamically in a single line of code.

Method 1 – Create a Dictionary from a List

Let’s say I have a list of U.S. states and I want to assign each state a code. Instead of writing a loop, I can use dictionary comprehension in Python.

states = ["California", "Texas", "Florida", "New York"]

# Create a dictionary with state name as key and length of state name as value
state_dict = {state: len(state) for state in states}

print(state_dict)

Output:

{'California': 10, 'Texas': 5, 'Florida': 7, 'New York': 8}

You can see the output in the screenshot below.

python dictionary comprehension

This is much shorter and cleaner than using a loop.

Method 2 – Filter Items in a Dictionary

Sometimes, I need to filter out certain key-value pairs. For example, let’s say I have a Python dictionary of U.S. cities and their populations, and I only want cities with more than 1 million people.

cities = {
    "New York": 8419600,
    "Los Angeles": 3980400,
    "Chicago": 2716000,
    "Houston": 2328000,
    "Phoenix": 1690000,
    "San Antonio": 1547000,
    "San Diego": 1424000,
    "Dallas": 1343000,
    "San Jose": 1035000,
    "Austin": 978908
}

# Filter cities with population > 1 million
large_cities = {city: pop for city, pop in cities.items() if pop > 1000000}

print(large_cities)

Output:

{'New York': 8419600, 'Los Angeles': 3980400, 'Chicago': 2716000,
 'Houston': 2328000, 'Phoenix': 1690000, 'San Antonio': 1547000,
 'San Diego': 1424000, 'Dallas': 1343000, 'San Jose': 1035000}

You can see the output in the screenshot below.

python dict comprehension

This is a real-world use case where dictionary comprehension makes filtering data super easy.

Method 3 – Swap Keys and Values

I often need to reverse a dictionary, where keys become values and values become keys. Python dictionary comprehension makes this simple.

state_codes = {
    "CA": "California",
    "TX": "Texas",
    "FL": "Florida",
    "NY": "New York"
}

# Swap keys and values
reversed_dict = {v: k for k, v in state_codes.items()}

print(reversed_dict)

Output:

{'California': 'CA', 'Texas': 'TX', 'Florida': 'FL', 'New York': 'NY'}

You can see the output in the screenshot below.

dictionary comprehension python

This method quickly flips keys and values, giving you a reversed dictionary with clean, readable code.

Method 4 – Use Conditional Expressions

You can also apply conditions inside a dictionary comprehension. For example, let’s classify U.S. states by whether their name length is even or odd.

states = ["California", "Texas", "Florida", "New York"]

# Classify states as Even or Odd based on name length
state_classification = {
    state: "Even" if len(state) % 2 == 0 else "Odd"
    for state in states
}

print(state_classification)

Output:

{'California': 'Even', 'Texas': 'Odd', 'Florida': 'Odd', 'New York': 'Even'}

You can see the output in the screenshot below.

dict comprehension

This method helps classify dictionary items neatly using conditions within comprehension.

Method 5 – Nested Dictionary Comprehension

Sometimes, I need to build nested dictionaries. For example, let’s create a multiplication table for numbers 1 to 5.

# Create a multiplication table using nested dictionary comprehension
multiplication_table = {
    i: {j: i * j for j in range(1, 6)}
    for i in range(1, 6)
}

print(multiplication_table)

Output (partial):

{
  1: {1: 1, 2: 2, 3: 3, 4: 4, 5: 5},
  2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10},
  3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15},
  ...
}

This is a powerful way to generate structured data.

Method 6 – Dictionary from Two Lists

Sometimes I have two lists, one with keys and one with values. I can combine them easily with dictionary comprehension.

states = ["California", "Texas", "Florida", "New York"]
abbreviations = ["CA", "TX", "FL", "NY"]

# Combine two lists into a dictionary
state_dict = {state: abbr for state, abbr in zip(states, abbreviations)}

print(state_dict)

Output:

{'California': 'CA', 'Texas': 'TX', 'Florida': 'FL', 'New York': 'NY'}

This method quickly pairs two lists into a clean dictionary using comprehension.

Method 7 – Dictionary Comprehension with Functions

I also use dictionary comprehension to apply functions to values. For example, converting temperatures from Fahrenheit to Celsius.

temps_f = {"New York": 86, "Los Angeles": 75, "Chicago": 70, "Houston": 95}

# Convert Fahrenheit to Celsius
temps_c = {city: round((f - 32) * 5/9, 2) for city, f in temps_f.items()}

print(temps_c)

Output:

{'New York': 30.0, 'Los Angeles': 23.89, 'Chicago': 21.11, 'Houston': 35.0}

This method applies a function to each value, making transformations simple and efficient.

Dictionary comprehension is one of those Python features that I use almost daily. It helps me write cleaner and faster.

Whether you’re filtering data, swapping keys and values, or building nested structures, dictionary comprehension can handle it all in just one line.

If you’re new to Python, I recommend practicing these examples with your datasets. Soon, you’ll find yourself using dictionary comprehension naturally in your projects.

You may also read other dictionary 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.