List and dictionary comprehensions in Python

In this tutorial, we will learn about the list and dictionary comprehensions in Python. We will begin with list comprehension and then we discuss dictionary comprehension.

List Comprehension

List comprehension is an elegant way of creating a new list from an existing list. Let us understand Python’s list comprehension using examples.

Example 1:

At first, we create dummy data. It is a list L of the first ten natural numbers. Using list comprehension, we create another list E which contains the even numbers from L.

`L = [1,2,3,4,5,6,7,8,9,10] # dummy dataE = [num for num in L if num%2 == 0]print(E)`

The output of the above print function is shown below:

`[2, 4, 6, 8, 10]`

In the above example, we have given a list L. From this list L, we have created another list E consisting of only the even numbers which are present in the list L. The same task can be done using a loop also. The corresponding codes are shown below:

`L = [1,2,3,4,5,6,7,8,9,10] # dummy dataE = [] # empty listfor num in L:     if num%2 == 0:     E.append(num)print(E)`

The output of the above print statement will be the same as earlier.

`[2, 4, 6, 8, 10]`

Hence, it is clear that Python list comprehension provides an elegant way of creating a new list from existing lists. Using Python’s list comprehension, we can write one-liner codes.

Example 2:

`L = [1,2,3,4,5,6,7,8,9,10] # dummy dataL2 = [num**2 for num in L if num%2 != 0]print(L2)`

The output of the print statement is mentioned below:

`[1, 9, 25, 49, 81]`

In this example, we have given the same list L. From this list L, we have created another list L2 consisting of the squared of the odd numbers which are present in list L. The same task can also be done using a loop. The corresponding codes are shown below:

`L = [1,2,3,4,5,6,7,8,9,10] # dummy dataL2 = [] # empty listfor num in L:     if num%2 != 0:     L2.append(num**2)print(L2)`

The output of this print statement is the same as shown earlier.

`[1, 9, 25, 49, 81]`

Dictionary Comprehension

Now, we will learn about Python dictionary comprehension. Dictionary comprehension is another elegant way of creating a new dictionary from an existing dictionary in Python. Let us understand Python’s dictionary comprehension using examples.

Example: 3

`dict_ft = {“Ram”: 5.5, “Shyam”: 6.1, “Amin”: 5.2}print(dict_ft)`

The output of the print statement is mentioned below:

`{‘Ram’: 5.5, ‘Shyam’: 6.1, ‘Amin’: 5.2}`

The above dictionary named dict_ft contains the names of three people as key values and their respective heights in feet. Now, using dictionary comprehension, we create a new dictionary named dict_cm which will contain the names of these people as key values, and their respective heights in centimeters.

`dict_cm = {key: round(value*30.48,2) for (key, value) in dict_ft.items()}# 1 foot = 30.48 cmprint(dict_cm)`

The output of the above print statement is shown below:

`{‘Ram’: 167.64, ‘Shyam’: 185.93, ‘Amin’: 158.5}`

Here, the heights of persons are in centimeters.

Example: 4

`dict_person = {“Lucas”: 45, “Stano”: 65, “Peter”: 59, “Hana”: 69, “Eva”: 23}print(dict_person)`

The output of the print function is given below:

`{‘Lucas’: 45, ‘Stano’: 65, ‘Peter’: 59, ‘Hana’: 69, ‘Eva’: 23}`

The above dictionary named dict_person contains the names of five persons as key values and their respective ages in years. Now, using dictionary comprehension with the if-else conditional statement, we create a new dictionary named dict_age which will contain the names of these persons as key values and classify them as either young or old. Any person who is above 60 years of age is considered old.

`dict_age = {key: (“Old” if value > 60 else “Young”) for (key, value) in dict_person.items()} print(dict_age)`

The output of the above print statement is mentioned below:

`{‘Lucas’: ‘Young’, ‘Stano’: ‘Old’, ‘Peter’: ‘Young’, ‘Hana’: ‘Old’, ‘Eva’: ‘Young’}`

This tutorial was initially published at https://soumenatta.blogspot.com/.

Assistant Professor at Indian Institute of Information Technology (IIIT) Vadodara, India, and Postdoctoral researcher at Masaryk University, Czech Republic.

More from Dr. Soumen Atta, Ph.D.

Assistant Professor at Indian Institute of Information Technology (IIIT) Vadodara, India, and Postdoctoral researcher at Masaryk University, Czech Republic.