Python Optimizations (Comprehensions)

Comprehensions in Python provide us with a short and concise way to construct new sequences (such as lists, set, dictionary etc.) using sequences which have been already defined. Python supports the following 4 types of comprehensions:

  • List Comprehensions

  • Dictionary Comprehensions

  • Set Comprehensions

  • Generator Comprehensions

List Comprehensions:

List Comprehensions provide an elegant way to create new lists. The following is the basic structure of a list comprehension:

output_list = [output_exp for var in input_list if (var satisfies this condition)]

Note that list comprehension may or may not contain an if condition. List comprehensions can contain multiple for (nested list comprehensions).

Example #1: Suppose we want to create an output list which contains only the even numbers which are present in the input list. Let’s see how to do this using for loops and list comprehension and decide which method suits better.

Program 1:

# Constructing output list WITHOUT 
# Using List comprehensions 
input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7] 
  
output_list = [] 
  
# Using loop for constructing output list 
for var in input_list: 
    if var % 2 == 0: 
        output_list.append(var) 
  
print("Output List using for loop:", output_list) 

Output:

Output List using for loop: [2, 4, 4, 6]

Program 2:

# Using List comprehensions 
# for constructing output list 
  
input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7] 
  
list_using_comp = [var for var in input_list if var % 2 == 0] 
  
print("Output List using list comprehensions:", list_using_comp)

Output:

Output List using list comprehensions: [2, 4, 4, 6]

Example #2: Suppose we want to create an output list which contains squares of all the numbers from 1 to 9. Let’s see how to do this using for loops and list comprehension.

Program 1:

# Constructing output list using for loop 
output_list = [] 
for var in range(1, 10): 
    output_list.append(var ** 2) 
      
print("Output List using for loop:", output_list) 

Output:

Output List using for loop: [1, 4, 9, 16, 25, 36, 49, 64, 81]

Program 2:

# Constructing output list 
# using list comprehension 
list_using_comp = [var**2 for var in range(1, 10)] 
  
print("Output List using list comprehension:", list_using_comp) 

Output:

Output List using list comprehension: [1, 4, 9, 16, 25, 36, 49, 64, 81]

Dictionary Comprehensions:

Extending the idea of list comprehensions, we can also create a dictionary using dictionary comprehensions. The basic structure of a dictionary comprehension looks like below.

output_dict = {key:value for (key, value) in iterable if (key, value satisfy this condition)}

Example #1: Suppose we want to create an output dictionary which contains only the odd numbers that are present in the input list as keys and their cubes as values. Let’s see how to do this using for loops and dictionary comprehension.

Program 1:

input_list = [1, 2, 3, 4, 5, 6, 7] 
  
output_dict = {} 
  
# Using loop for constructing output dictionary 
for var in input_list: 
    if var % 2 != 0: 
        output_dict[var] = var**3
  
print("Output Dictionary using for loop:", output_dict ) 

Output:

Output Dictionary using for loop: {1: 1, 3: 27, 5: 125, 7: 343}

Program 2:

# Using Dictionary comprehensions 
# for constructing output dictionary 
  
input_list = [1,2,3,4,5,6,7] 
  
dict_using_comp = {var:var ** 3 for var in input_list if var % 2 != 0} 
  
print("Output Dictionary using dictionary comprehensions:", dict_using_comp) 

Output:

Output Dictionary using dictionary comprehensions: {1: 1, 3: 27, 5: 125, 7: 343}

Example #2: Given two lists containing the names of states and their corresponding capitals, construct a dictionary which maps the states with their respective capitals. Let’s see how to do this using for loops and dictionary comprehension.

Program 1:

state = ['Gujarat', 'Maharashtra', 'Rajasthan'] 
capital = ['Gandhinagar', 'Mumbai', 'Jaipur'] 
  
output_dict = {} 
  
# Using loop for constructing output dictionary 
for (key, value) in zip(state, capital): 
    output_dict[key] = value 
  
print("Output Dictionary using for loop:", output_dict) 

Output:

Output Dictionary using for loop: {'Gujarat': 'Gandhinagar', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}

Program 2:

# Using Dictionary comprehensions 
# for constructing output dictionary 
  
state = ['Gujarat', 'Maharashtra', 'Rajasthan'] 
capital = ['Gandhinagar', 'Mumbai', 'Jaipur'] 
  
dict_using_comp = {key:value for (key, value) in zip(state, capital)} 
  
print("Output Dictionary using dictionary comprehensions:",  
                                           dict_using_comp) 

Output:

Output Dictionary using dictionary comprehensions: {'Rajasthan': 'Jaipur', 'Maharashtra': 'Mumbai', 'Gujarat': 'Gandhinagar'}

Set Comprehensions:

Set comprehensions are pretty similar to list comprehensions. The only difference between them is that set comprehensions use curly brackets { }. Let’s look at the following example to understand set comprehensions.

Example #1 : Suppose we want to create an output set which contains only the even numbers that are present in the input list. Note that set will discard all the duplicate values. Let’s see how we can do this using for loops and set comprehension.

Program 1:

input_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7] 
  
output_set = set() 
  
# Using loop for constructing output set 
for var in input_list: 
    if var % 2 == 0: 
        output_set.add(var) 
  
print("Output Set using for loop:", output_set) 

Output:

Output Set using for loop: {2, 4, 6}

Program 2:

# Using Set comprehensions  
# for constructing output set 
  
input_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7] 
  
set_using_comp = {var for var in input_list if var % 2 == 0} 
  
print("Output Set using set comprehensions:", set_using_comp) 

Output:

Output Set using set comprehensions: {2, 4, 6}

Generator Comprehensions:

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. The major difference between them is that generators don’t allocate memory for the whole list. Instead, they generate each value one by one which is why they are memory efficient. Let’s look at the following example to understand generator comprehension:

Syntax Difference: Parenthesis are used in place of square brackets.

List Comprehension:

list_comprehension = [i for i in range(11) if i % 2 == 0] 
  
print(list_comprehension)

0 2 4 6 8 10

Generator Comprehension:

generator_expression = (i for i in range(11) if i % 2 == 0) 
  
print(generator_expression)

<generator object at 0x000001452B1EEC50>

In the above example, if we want to print the output for generator expressions, we can simply iterate it over generator object.

for i in generator_expression: 
    print(i, end=" ")  

0 2 4 6 8 10

So what’s the difference between Generator Expressions and List Comprehensions? The generator yields one item at a time and generates item only when in demand. Whereas, in a list comprehension, Python reserves memory for the whole list. Thus we can say that the generator expressions are memory efficient than the lists.

Last updated