What are list comprehensions in Python? Are there other types of comprehensions?

  • How many types of comprehensions are there in Python?
  • What is the Syntax for comprehensions?
  • When were these different comprehensions introduced in Python?

Consider the following few lines of code:
Input:

iterable = [1,2,3,4]
new_list = []
for item in iterable:
if item%2 == 0:
new_list.append(item**2)
print(new_list)

Output:

[4, 16]

What if the above 3 lines of codes consisting of for loop, the if condition and .append() function could be converted to one line of code? That is where comprehensions come into play and how each type works is explained below.

There are essentially four type of comprehensions in Python:

  1. List Comprehensions ( introduced in Python 2.0)
  2. Dictionary Comprehensions
  3. Set Comprehensions
  4. Generator Comprehensions ( introduced in Python 2.4)

Python 2.7 and 3.0, introduces dictionary comprehensions, as well as the closely related set comprehensions



It is to be noted that the iterable in each type of comprehension iterable can be any iterable object such as lists, tuples, strings, range etc.
Also, the if condition is optional.



List Comprehensions
List Comprehensions provide an elegant and quick way to construct new lists from existing iterables.
The Syntax is as follows:

new_list = [expression for item in iterable if condition == True]

In this way, a new list is created while leaving the existing iterable unchanged.

Let us understand with the help of an example where the iterable is a list:

Input:

iterable = [1,2,3,4]
new_list = [i**2 for i in iterable if i%2 == 0]
print(new_list)

Output:

[4, 16]

Here, the each item in the existing_list was squared if that item was divisible by two.

Given below is another example where iterable is a string:
Input:

string = 'foo'
new_list = [i for i in string]
print(new_list)

Output:

['f', 'o', 'o']



Dictionary Comprehensions
Similar to list comprehensions, Dictionary Comprehensions provide an elegant and quick way to construct new dictionaries from existing iterables.
The Syntax is as follows:

new_dict = [key:value for item in iterable if condition == True]


Look at the following example:

Input:

iterable = [1,2,3,4]
new_dict = {i:i**2 for i in iterable if i%2 == 0}
print(new_dict)

Output:

{2: 4, 4: 16}



Set Comprehensions
Again, following the same idea, Set Comprehensions provide an elegant and quick way to construct new dictionaries from existing iterables.
The Syntax is as follows:

new_set = {expression for item in iterable if condition == True}


Let us look at a couple of examples:

Input:

iterable = [1,2,3,4]
new_set = {i**2 for i in iterable if i%2 == 0}
print(new_set)

Output:

{4, 16}

Input:

string = 'foo'
new_set = {i for i in string}
print(new_set)

Output:

{'f', 'o'}



Generator Comprehensions
Generator comprehensions are similar to list comprehensions. Instead of creating lists and keeping the whole sequence in the memory, the generator generates the next element in demand, hence are memory efficient.
The Syntax difference is that parenthesis () are used instead of square brackets [].
The Syntax is as follows:

new_gen = (expression for item in iterable if condition == True)


Let us look at an example:

Input:

iterable = [1,2,3,4]
new_gen = (i**2 for i in iterable if i%2 == 0)
print(new_gen)

Output:

<generator object <genexpr> at 0x7f124aabd5d0>

By simply iterating over the generator object we can print the required/desired output:

Input:

for i in new_gen:
print(i)

Output:

4
16




The main thing to be understood here is that Python offers programmers to write concise, efficient and easy-to-read code. Comprehensions are a major feature offering a single line of code and hence increasing the practicality of the code being written.

2 Likes