Python Comprehension

Mayank Porwal
3 min readOct 26, 2020
python comprehension
Photo by Hitesh Choudhary on Unsplash

What is comprehension??

Comprehension in python is a short and elegant way to produce a list(or any sequence like dictionary, sets) using python list(or any sequence like dictionary, sets).

There are three mostly use comprehension in python:

  1. List Comprehension

2. Dictionary Comprehension

3. Set Comprehension

List Comprehension:

List comprehension is a short and efficient way to create list.

The basic syntax of list comprehension:

[expression for item in list if (item satisfied this condition)]

In below example we will see why list comprehension is a efficient way to create list.

Example: 1(a) Create a list using for loop

# python list is iterable
new_list = []
for i in "impossible":
new_list.append(i)
print(new_list)

Output:

['i', 'm', 'p', 'o', 's', 's', 'i', 'b', 'l', 'e']

Example: 1(b) Create a list using list comprehension

[i for i in “impossible”]

When we run the code the output will be

['i', 'm', 'p', 'o', 's', 's', 'i', 'b', 'l', 'e']

Condition in Python List Comprehension:

Example: 2(a)

[i for i in range(10) if i%2 !=0]

When we run the code the output will be

[1, 3, 5, 7, 9]

In the above python code takes the value in range(10) and return a list with odd values.

Example: 2(b)

[i for i in range(10) if i%2==0 if i%3==0]

When we run the code the output will be

[0, 6]

In the above python code takes the value in range(10) and return a list with values which is divisible by 2 and 3.

Dictionary Comprehensions:

The idea behind the dictionary comprehensions is same as list comprehension. We can also create dictionary using dictionary comprehension.

Syntax of dictionary comprehension look like:

{key:value for (key, value) in iterable if (condition)}

Example 1(a):

Suppose we have two lists of city with their respective state. Now we want to create a dictionary which maps state and their respective cities.

Using for loop:

state = ['Rajasthan', 'UP', 'Bihar']
city = ['Jaipur', 'Kanpur', 'Patna']
output = {}
for (key, value) in zip(state, city):
output[key] = value
print(output)

When we run the code the output will be

{'Rajasthan': 'Jaipur', 'UP': 'Kanpur', 'Bihar': 'Patna'}

Now using dictionary comprehension:

state = ['Rajasthan', 'UP', 'Bihar']
city = ['Jaipur', 'Kanpur', 'Patna']
{ i:j for (i,j) in zip(state, city)}

When we run the code the output will be

{'Rajasthan': 'Jaipur', 'UP': 'Kanpur', 'Bihar': 'Patna'}

Don’t worry about zip() function.

zip() function takes iterables(list, dictionary, string etc.) as arguments and aggregate them in a tuple and return it.

Set Comprehensions:

Set comprehension are pretty much similar to list comprehensions. Main difference is that set comprehensions uses curly brackets {}.

Example 1(a):

Suppose we have a list and want to create a set of square of each values in that list.

Using for loop:

new_list = [1,2,3,4,5,6,7,8]
a = set()
for i in new_list:
a.add(i**3)
print(a)

When we run the code the output will be

{64, 1, 512, 8, 343, 216, 27, 125}

Using set comprehensions:

new_list = [1,2,3,4,5,6,7,8]
{ i**3 for i in new_list }

When we run the code the output will be

{1, 8, 27, 64, 125, 216, 343, 512}

Thanks for reading…….

Feel free to leave a comment or share this post. Follow me for future posts….

--

--