Python Dictionary Comprehension (With Examples)

John John (304)
5 minutes

In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way. Many people are familiar with Python list comprehension, but the comprehension syntax can be used to create dictionaries as well. In this guide, I'll explain the syntax, walk through a few examples of how dictionary comprehension works, and show how it can be used to simplify your code.

Posted in these interests:
h/python67 guides
h/code69 guides

If you're new to comprehensions, this syntax might be a little confusing. So I'll break it down:

{key: value for x in iterable}

A conditional can optionally included like this:

new_dict = {key: value for x in iterable if condition}

In many use cases, the elements in the iterable will provide the key and value, but it's not required. We iterate over iterable assigning each item to the variable x. Then the new dictionary is formed using the variables key and value.

Here's a simple example:

> {str(x): x for x in range(3)}
{'0': 0, '1': 1, '2': 2}

The following shows how our example maps to the syntax instruction from above.

Syntax Example
key str(x)
value x
iterable range(3)

In the remainder of this guide, I'll provide examples of how dictionary comprehensions can be used to improve your code.

Sometimes we have a list of dictionaries, and we need to lookup a dictionary by a particular key. This can be the case with database query results or data read from a csv.

Goal: Convert a list of dictionaries to a nested dictionary, so we can look up elements by their id.

The initial data is structured like this:

data_list = [
    {'id': '1', 'name': ...},
    {'id': '2', 'name': ...},
    {'id': '3', 'name': ...},
    ...
]

If we need to lookup rows by id, we could search the list, but that would be inefficient for multiple lookups. A better way is to convert the list of dictionaries to a dictionary, where the key is the id.

data_dict = {
    '1': {'id': '1', 'name': ...},
    '2': {'id': '2', 'name': ...},
    '3': {'id': '3', 'name': ...},
}

Now we can easily and efficiently lookup dictionaries by their id.

some_dict = data_dict[id]

So how do we accomplish this?

Using a for loop

data_dict = {}

for item in data_list:
    data_dict[item['id']] = item

We iterate over the list, grabbing the id from each item. Then we create a new dictionary key and value during each iteration.

This is not too bad, but we can simplify even more using dictionary comprehension.

Using dictionary comprehension

data_dict = {d['id']: d for d in data_list}

Hopefully you can see how both examples accomplish the same thing. They both include the same components, but the second example is cleaner.

Suppose we have a dictionary with many key-value pairs, and we want to copy that dictionary and filter out specific values.

Goal: Create a dictionary of U.S. state data including only states established on or after 1900.

Our initial dictionary contains data for all 50 U.S. states. It looks like this:

state_data = {
    'Alabama': {   
        'Abbreviation': 'AL',
        'Capital': 'Montgomery',
        'Established': 1819
    },
    'Alaska': {
        'Abbreviation': 'AK',
        'Capital': 'Juneau',
        'Established': 1959
    },
    ...
}

So we want to filter our dictionary and include only states that were established on or after 1900.

Using a for loop

states_after_1900 = {}

for k, v in state_data.items():
    if v['Established'] >= 1900:
        states_after_1900[k] = v

This gives us the answer we'd like, but with dictionary comprehensions we can accomplish this in a single line of code.

Using dictionary comprehension

states_after_1900 = {k: v for k, v in state_data.items() if v['Established'] >= 1900}

Notice this includes all of the same components, but it's much more concise.

The results

In order to conserve space, while still proving this worked as expected, I'll just print the state and the date it was established:

for k, v in states_after_1900.items():
    print(k, v['Established'])

# => Output

Alaska 1959
Arizona 1912
Hawaii 1959
New Mexico 1912
Oklahoma 1907

In some cases, we start with a dictionary that is structured in a specific way, but we need the values to be transformed.

I'm going to use a contrived example to demonstrate how this works. Suppose we have a dictionary, where the key is a user id, and the value is the user's first name. For some reason, we want to display the users names in reverse, possibly multiple times. We could use the extended slice operator or the reversed function as needed, but that could be wasteful if we're planning on doing this multiple times for each name. Another option is to create a new dictionary where the names are reversed.

Note: For more details on reversing strings in Python check out our guide.

Here's the initial names dictionary.

names = {
    1: 'Sam',
    2: 'Clark',
    3: 'Carol',
    ...
}

We'll create a new dictionary where each value is mapped to it's reverse.

Using a for loop

names_reversed = {}

for k, v in names.items():
    names_reversed[k] = v.reversed()

Using dictionary comprehension

names_reversed = {k: v[::-1] for k, v in names.items()}

Results

names_reversed

=> Output

{1: 'maS', 2: 'kralC', 3: 'loraC'}
Learn how to use formatted string literals in Python
John John (304)
5 minutes

Python supports multiple ways of formatting strings, but Python 3.6 (PEP 498) introduced a new, concise way to embed expressions inside of strings literals.