Python dictionary comprehension is a concise and efficient way to create dictionaries. It is a technique that allows users to create a new dictionary by iterating over a sequence and defining the key-value pairs based on the given conditions. This feature is available in Python 2.7 and all later versions.
In this tutorial, we will explain the concept of dictionary comprehension and provide examples to illustrate its usage.
What is Python Dictionary Comprehension?
Python dictionary comprehension is a syntactic construct that allows users to create a new dictionary in a single line of code. It uses a compact and readable syntax to create dictionaries based on existing sequences such as lists, tuples, or sets.
The syntax for dictionary comprehension is as follows:
{key:value for (key, value) in iterable}
where iterable
is any sequence such as lists, tuples, or sets.
The key
and value
can be any valid Python expression, and the for
loop iterates over the elements of the iterable to create a new dictionary.
Examples of Python Dictionary Comprehension
Let’s look at some examples to understand the concept of dictionary comprehension in Python.
Example 1: Create a dictionary from two lists
Suppose we have two lists, one containing the names of fruits and the other containing the corresponding prices. We can create a dictionary with the help of dictionary comprehension.
fruits = ['apple', 'banana', 'mango']
prices = [0.5, 0.25, 1.0]
fruit_dict = {fruits[i]: prices[i] for i in range(len(fruits))}
print(fruit_dict)
Output:
{'apple': 0.5, 'banana': 0.25, 'mango': 1.0}
Example 2: Filter elements while creating a dictionary
We can also filter the elements of the iterable while creating a dictionary. For example, let’s create a dictionary of even numbers from 1 to 10.
even_dict = {i: i**2 for i in range(1, 11) if i % 2 == 0}
print(even_dict)
Output:
{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
Example 3: Create a dictionary from a tuple of tuples
We can also create a dictionary from a tuple of tuples using dictionary comprehension. For example,
tup = (('apple', 0.5), ('banana', 0.25), ('mango', 1.0))
fruit_dict = {x: y for x, y in tup}
print(fruit_dict)
Output:
{'apple': 0.5, 'banana': 0.25, 'mango': 1.0}
Example 4: Create a dictionary with default values
We can create a dictionary with default values using dictionary comprehension. For example, let’s create a dictionary with the keys as the names of fruits and default values as 0.
fruits = ['apple', 'banana', 'mango']
fruit_dict = {x: 0 for x in fruits}
print(fruit_dict)
Output:
{'apple': 0, 'banana': 0, 'mango': 0}
Example 5: Create a dictionary from a string
We can also create a dictionary from a string using dictionary comprehension. For example, let’s create a dictionary with the frequency of each character in a string.
string = 'hello world'
freq_dict = {i: string.count(i) for i in string}
print(freq_dict)
Output:
{'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
Conclusion
Python dictionary comprehension is a powerful and efficient way to create dictionaries. It allows users to create dictionaries in a concise and readable way, which can save time and effort. In this tutorial, we explained the concept of dictionary comprehension and provided examples to illustrate its usage. We hope that this tutorial has helped you understand the concept of dictionary comprehension in Python.