Python Dictionary Comprehension


Python Dictionary Comprehension

In Python, Dictionary Comprehension is used to create a new dictionary from a given iterable.

Dictionary Comprehension offers a shorter syntax to create dictionaries from an iterable, unlike looping statements. Using dictionary comprehension, we can also include conditions or transformations while creating dictionaries.

In this tutorial, you will learn the syntax of Dictionary Comprehension, and go through different scenarios to understand how Dictionary Comprehension can be written or used in Python, with example programs.

Syntax of Dictionary Comprehension

The syntax to create a new dictionary from an iterable using Dictionary Comprehension technique is given below.

[key_expression: value_expression for item in iterable if condition]
  • key_expression is the expression that computes the keys for the new dictionary.
  • value_expression is the expression that computes the values for the new dictionary.
  • item is a variable that represents each item in the iterable you are iterating over.

The for loop is used to iterate over each item in the iterable, and if the item satisfies the given condition, you can transform the item using key_expression and value_expression and add the key-value pair to the dictionary.

if condition is optional. If this part is not given, then all items from the iterable shall be considered to be included in the dictionary.

Dictionary Comprehension in its simplest form

In this example, we shall copy elements of a dictionary to another dictionary. This is a great example to get started with Dictionary Comprehension in its simplest form in Python.

In the following program, we take a dictionary in input. This is our input iterable in the dictionary comprehension. We shall add each of the item in this iterable input to the new dictionary output using dictionary comprehension.

Python Program

input = {
    'foo': 12,
    'bar': 14,
    'moo': 16
}

output = {key: value for key, value in input.items()}
print(output)

Output

{'foo': 12, 'bar': 14, 'moo': 16}

Apply if-condition to Dictionary Comprehension in Python

Now, let us apply a condition whether to add the element from the given dictionary to the new dictionary.

Let us apply a condition that the item from input dictionary shall be added to the new dictionary if the value is not 14.

Python Program

input = {
    'foo': 12,
    'bar': 14,
    'moo': 16
}

output = {key: value for key, value in input.items() if value != 14}
print(output)

Output

{'foo': 12, 'moo': 16}

The new dictionary does not contain the items from the original dictionary whose value is 14.

Now, let us apply a condition on key of the item in dictionary. Let us not include the item in the new dictionary if the key contains the character '0'.

Python Program

input = {
    'foo': 12,
    'bar': 14,
    'moo': 16
}

output = {key: value for key, value in input.items() if 'o' not in key}
print(output)

Output

{'bar': 14}

Both the keys: 'foo' and 'moo' has the character 'o', and the condition returns False for these two keys. Therefore they are not included in the new dictionary.

Dictionary Comprehension with keys from List and default value for all the keys

The iterable in the Dictionary Comprehension may not be a dictionary all the time. It can be list as well.

In the following program, we take a list of strings keys_list that are used as keys for the new dictionary output. We shall take a default value of 0 for all these keys.

Python Program

keys_list = ['foo', 'bar', 'moo']

output = {key: 0 for key in keys_list}
print(output)

Output

{'foo': 0, 'bar': 0, 'moo': 0}

Summary

In this tutorial, we got introduced to Dictionary Comprehension in Python. There are many other scenarios where Dictionary Comprehension is used, and how it is used. We shall go through all of them in our following tutorials.