The dictionary comprehension offers shorter syntax to create a new dictionary from the existing dictionary or any iterable (List, Tuple, or Set).
A comprehensions consist of a single expression followed by at least one for statement and zero or more for or if statements.
While using dictionary comprehension, your for-loop code should be enclosed in curly braces.
It will return a new dictionary while keeping the old dictionary unchanged when using dictionary comprehension.
new_dictionary = {key: value for item in iterable if condition == True}
key: This is the dictionary items key you want to include in the new dictionary for each item in the iterable.
value: This is the operation or value you want to include in the new dictionary for each item in the iterable.
item: This represents each individual item in the iterable.
iterable: This is the original collection of items you're iterating over (e.g., a list, tuple, set, dictionary, or string).
condition (optional): You can include a condition to filter items based on certain criteria. Only items that satisfy the condition will be included in the new dictionary.
Identify fruit names whose color name is "Red"
in the dictionary.
Without dictionary comprehension, you need to write a for
loop containing a condition statement.
fruits_dictionary = {
"apple": "red",
"banana": "yellow",
"strawberry": "red",
"grapes": "green",
"orange": "orange",
"pineapple": "yellow"
}
fruits_filter_dictionary = dict()
for fruit in fruits_dictionary:
color = fruits_dictionary[fruit]
if color == "red":
fruits_filter_dictionary[fruit] = color
print(fruits_filter_dictionary)
{
'apple': 'red',
'strawberry': 'red'
}
In the above example, we are identifying the fruit names that color is "Red"
and adding them to the new dictionary.
But with dictionary comprehension, we can do these things in one single line of code.
fruits_dictionary = {
"apple": "red",
"banana": "yellow",
"strawberry": "red",
"grapes": "green",
"orange": "orange",
"pineapple": "yellow"
}
fruits_filter_dictionary = {fruit: fruits_dictionary[fruit] for fruit in fruits_dictionary if fruits_dictionary[fruit] == "red"}
print(fruits_filter_dictionary)
{
'apple': 'red',
'strawberry': 'red'
}
Or, you can also fetch the key-value pairs of a dictionary by using the items()
function in the dictionary comprehension.
fruits_dictionary = {
"apple": "red",
"banana": "yellow",
"strawberry": "red",
"grapes": "green",
"orange": "orange",
"pineapple": "yellow"
}
fruits_filter_dictionary = {fruit: color for (fruit, color) in fruits_dictionary.items() if color == "red"}
print(fruits_filter_dictionary)
{
'apple': 'red',
'strawberry': 'red'
}
Here, we identified the fruit name that color as "Red"
with the dictionary comprehension.
As you can see, both outputs are the same with and without dictionary comprehension.
It is optional to include the condition in a dictionary comprehension.
The key-value pair represents the current item in the iteration it can manipulate before adding a new dictionary.
fruits_dictionary = {
"apple": "red",
"banana": "yellow",
"strawberry": "red",
"grapes": "green",
"orange": "orange",
"pineapple": "yellow"
}
fruits_filter_dictionary = {fruit.upper(): color.upper() for (fruit, color) in fruits_dictionary.items() }
print(fruits_filter_dictionary)
{
'APPLE': 'RED',
'BANANA': 'YELLOW',
'STRAWBERRY': 'RED',
'GRAPES': 'GREEN',
'ORANGE': 'ORANGE',
'PINEAPPLE': 'YELLOW'
}