Menu Close

Comprehension in Python

In this tutorial, you will learn about comprehension. Comprehension in Python provides a short and concise way to create a sequence (such as list, dictionary, set, etc).
Python support four types of comprehension which are a list, set, dictionary, and Generators Comprehensions.

Before going through this Comprehension in Python article, please read the following articles.

What is Comprehension?

Comprehension in Python provides a short and concise way to create a sequence like a list, dictionary, set and generators, etc.

Python support 4 types of comprehension.

  • List Comprehension
  • Set Comprehension
  • Dictionary Comprehension
  • Generator Comprehension

Let’s understand all the Python comprehension one by one along with example.

List Comprehension:

List comprehension provides a concise way to create a new list. list comprehension use square bracket ( [ ] ).

Syntax of List Comprehension

[expression for var in list]

Example:

Suppose we want to create an output list that contains only the even number from 1 to 10 using another list.
Then we have two ways to create output lists first is using Python for loop and another is using list comprehension.

  1. Create new list without list comprehension.
old_list = [1,2,3,4,5,6,7,8,9,10]
new_list = []

for var in old_list:
	if var % 2 == 0:
		new_list.append(var)
		
print("Output list without list comprehension: ",new_list)

Output

Output list without list comprehension:  [2, 4, 6, 8, 10]

2. Create new list using list comprehension.

old_list = [1,2,3,4,5,6,7,8,9,10]
new_list = [var for var in old_list if var % 2 == 0]
print("Output list using list comprehension: ",new_list)

Output

Dictionary Comprehension

Dictionary comprehension looks like list comprehension. Dictionary comprehension provides a facility to create a new dictionary.
Dictionary comprehension using an inside curly bracket ( { } ).

A dictionary comprehension takes the form {key: value for (key, value) in iterable}.

Syntax of Dictionary Comprehension:

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

Example:

Suppose we want to create new dictionary which contains square of numbers who exactly divisible by 3 between 1 to 20.

  1. Create new dictionary without dictionary comprehension.
old_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
new_dict = {}

for var in old_list:
	if var % 3 == 0:
		new_dict[var] = var * var

print("New dictionary without dictionary comprehension: ",new_dict)

Output

New dictionary without dictionary comprehension:  {3: 9, 6: 36, 9: 81, 12: 144, 15: 225, 18: 324}

2. Create new dictionary without dictionary comprehension.

old_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
new_dict = {var : var * var for var in old_list if var % 3 == 0}
print(new_dict)

Output

{3: 9, 6: 36, 9: 81, 12: 144, 15: 225, 18: 324}

you can check type of new dictionary using type() function.

print(type(new_dict))

Set comprehension:

set comprehension is the same as list comprehension. The difference between list comprehension and set comprehension is list comprehension uses a square bracket but set comprehension uses a curly bracket.

Syntax of set comprhension

{expression for var in list}

Example:

Suppose we want to create new list which contains numbers who exactly divisible by 2.

  1. create new set without set comprehension.
old_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7] 
new_set = set()

for var in old_list:
	if var % 2 == 0:
		new_set.add(var)
		
print("Output set of without set comprehension: ",new_set)

Output

Output set of without set comprehension:  {2, 4, 6}

You can check data type of new_set using type() function.

a = type(new_set)
print(a)

2. create new set using set comprehension.

old_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7] 
new_set = {var for var in old_list if var % 2 == 0}
print("Output of set comprehension: ",new_set)

Output

Output of set comprehension:  {2, 4, 6}

Generator Comprehension:

Generator Comprehensions are very similar to list comprehensions. One difference between them is that generator comprehensions use circular brackets whereas list comprehensions use square brackets.

old_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7] 
new_set = (var for var in old_list if var % 2 == 0)
print("The Output of generator comprehension: ",list(new_set))

Output

The output of generator comprehension:  [2, 4, 4, 6, 6, 6]

Conclusion

In this article, you have learned all about Comprehension in Python. Comprehension in Python is the best way to create a list, set, Dictionary, and Generator comprehension.

If you like this Comprehension in Python, please share and keep visiting for further Python tutorials.

Happy Coding
~ Vishvajit Rao

For more Information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply