Python's Dictionary Comprehension
Python's Dictionary Comprehension (with 39 Code Examples)
Dictionary comprehension is an elegant and concise way to create new dictionary from an iterable in Python. This tutorial will guide you through the concept and give you 39 examples of dictionary comprehension. So let’s get started.
What is Dictionary Comprehension in Python?
Dictionary comprehension is a method for transforming one dictionary into another dictionary. It is analogous to list comprehension, which is used for creating lists. With dictionary comprehension, you can create a new dictionary with fewer lines of code compared to normal dictionary creation.
In dictionary comprehension, each key: value pair gets its own line. The syntax is similar to list comprehension as well, but since dictionaries are unordered, you need to specify both a key and a value. Here is an example of a simple dictionary comprehension:
my_dict = {i: i*2 for i in range(10)}
print(my_dict)
The resulting dictionary in this example would be {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}. You can see that the keys are the numbers from 0 to 9 and the values are the corresponding keys multiplied by two.
Another plus of using dictionary comprehension is that it allows you to filter the keys and values. For example, if you wanted only the keys from 1 to 5, you could use the following expression:
my_dict = {i: i*2 for i in range(5)}
print(my_dict)
This expression would result in {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}. As you can see, only the keys from 1 to 5 were included in the dictionary.
Python Dictionary Comprehension Examples
Now that you know what dictionary comprehension is and how it works, let’s take a look at some examples. The first example will demonstrate how to create a dictionary with values greater than a certain number.
my_dict = {i: i*2 for i in range(10) if i > 5}
print(my_dict)
This expression would result in {6: 12, 7: 14, 8: 16, 9: 18}. You can see that only the keys from 6 to 9 were included in the dictionary since they are the ones that satisfy the condition (i > 5).
You can also use dictionary comprehension to create dictionaries with multiple keys and values. For example, if you wanted to create a dictionary with the squares of both even and odd numbers, you could do it like this:
my_dict = {i: i**2 for i in range(10) if i % 2 == 0 or i % 2 == 1}
print(my_dict)
The resulting dictionary in this example would be {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}. You can see that only the keys from 0 to 9 were included in the dictionary since they are the ones that satisfy the condition (i % 2 == 0 or i % 2 == 1).
You can also use dictionary comprehension to create dictionaries from other dictionaries. In this example, we are going to create a new dictionary from an existing one that contains employee's name and their salary:
employees = {
"John": 5000,
"Mary": 3000,
"Bob": 2000
}
new_dict = {k: v * 1.1 for k, v in employees.items()}
print(new_dict)
The resulting dictionary in this example would be {'John': 5500.0, 'Mary': 3300.0, 'Bob': 2200.0}. You can see that the salaries of the employees were increased by 10%.
Summary
In this tutorial, we have learned about dictionary comprehension and its uses. We have seen several examples of how to use dictionary comprehension to create new dictionaries from existing ones. We have also seen how to filter the keys and values of the dictionaries using conditions. Dictionary comprehension can be a powerful tool when used correctly and can help you write more concise code.