Python Dictionary Comprehension

 

In this tutorial I am going to talk about Python Dictionary Comprehension. 

If you want to learn more about Python Dictionary, you can look at my free Python Tutorial . 
 

 

First, I am going to talk about what is Python Dictionary. Then I am going to talk about Pyhon Dictionary Comprehension with examples. 

 

Python Dictionary

Dictionary is an unordered, changeable and indexed collection of items.  Dictionaries have key and value.

With dictionary, you can do many task that require a lot of code with a single line of code.

Let’s give an example of using dictionary in real life.

You don’t know the phone number of everyone in your phone book. You save the number with the name of the person so that you can access and call the person number with the name at any time.

This is also the same for the python dictionary. Python also consists of dictionary key and value items. We use curly brackets to create a dictionary in Python.

The Python dictionary syntax structure is as follows:

my_dictionary = { "key1": value1, "key2": value2 } 
If you want to learn more about Python dictionary, you can go to my Dictionaries in Python tutorial.
 
Now, Let’s learn about dictionary comprehension in Python.
 
 
 
 
 

Python Dictionary Comprehension

 
Dictionary comprehension is a special and elegant way to create dictionaries.
 
We can create dictionaries using simple expressions and dictionary comprehension should be written in a specific pattern.
 
The syntax of dictionary comprehension  :
{ key : value for ( key, value ) in dictionary.items() }  

Why Use Dictionary Comprehension

 

First of all, Let’s start explaining why we use comprehension.

Example : 

Without comprehension:

values = [] # empty list

for x in range(5):
values.append(x * 2)

print(values) 

Output : 

[0, 2, 4, 6, 8] 

 

 

With Comprehension:

values = {x: x * 2 for x in range(5)}

print(values) 

Output : 

{0: 0, 1: 2, 2: 4, 3: 6, 4: 8} 
As you can see we can do the same  program with just a line of code with comprehension.
 
 
Now, Let’s look at other dictionary comprehension examples.
 
 
 
Example : 
 
Let say that we want to change the product prices from dollar to euro. Here is the program with dictionary comprehension.
my_dictionary = {"computer": 900, "phone": 400, "book": 10}
dollar_to_euro = 0.84

new_price = {key: value * dollar_to_euro for(key, value) in my_dictionary.items()}

print(new_price) 

Output :

{'computer': 756.0, 'phone': 336.0, 'book': 8.4} 

Using Conditional in Python Dictionary

Python Dictionary If Condition

 
Example :
 
In this example, if the person’s age bigger or equal to 18, we will add it to our new dictionary. 
my_dictionary = {"James": 25, "John": 17, "Adam": 36}

new_dictionary = {k : v for (k,v) in my_dictionary.items() if v >= 18}

print(new_dictionary) 

 

Output:

{'James': 25, 'Adam': 36} 

Python Dictionary Multiple Condition

 
Example :
 
In the same example if the person’s age is  bigger or equal to 18 and divided by 2, then we will add it to our new dictionary . 
my_dictionary = {"James": 25, "John": 17, "Adam": 36}

new_dictionary = {k : v for (k,v) in my_dictionary.items() if v % 2 == 0 if v >= 18}

print(new_dictionary) 

Output:

{'Adam': 36} 

Python Dictionary Comprehension If Else Condition

 
Example: 
 
In the same example if the person’s age is bigger or equal to 18, our program will add it as ” can vote ” otherwise “cannot vote“.  
my_dictionary = {"James": 25, "John": 17, "Adam": 36}

new_dictionary = {k : ('can vote' if v >=18 else 'cannot vote') for (k,v) in my_dictionary.items() }

print(new_dictionary) 

Output:

{'James': 'can vote', 'John': 'cannot vote', 'Adam': 'can vote'} 

 

As you can see we can write better, cleaner code with dictionary comprehension. In short, This is more Pythonic.

Leave a Reply

Your email address will not be published. Required fields are marked *