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:
Output:
Output List using for loop: [2, 4, 4, 6]
Program 2:
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:
Output:
Output List using for loop: [1, 4, 9, 16, 25, 36, 49, 64, 81]
Program 2:
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:
Output:
Output Dictionary using for loop: {1: 1, 3: 27, 5: 125, 7: 343}
Program 2:
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:
Output:
Output Dictionary using for loop: {'Gujarat': 'Gandhinagar', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}
Program 2:
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:
Output:
Output Set using for loop: {2, 4, 6}
Program 2:
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:
0 2 4 6 8 10
Generator Comprehension:
<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.
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