List and dictionary comprehensions in Python

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.

The output of the above print function is shown below:

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:

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

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:

The output of the print statement is mentioned below:

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:

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

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

The output of the print statement is mentioned below:

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.

The output of the above print statement is shown below:

Here, the heights of persons are in centimeters.

Example: 4

The output of the print function is given below:

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.

The output of the above print statement is mentioned below:

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store