Comprehensions in Python

  • (4.0)
  • | 959 Ratings |
  • Last Updated January 10, 2019

Lists are important tools for describing any data in a concise format. In other words, they improve the understanding and manipulation process of data. Python programming language provides us with the functionality of listing items. The programming codes get shorter and easily decipherable, hence reducing the chances of the error on one hand and increasing the execution pace of the program. 

If you are well familiar with the list functions in Python, then the list comprehensions can be handy for coding as they help in making new syntax for any problem in Python. Here, in this list comprehension python tutorial, we will explore List comprehensions and understand the importance of using them in our Python code. So let us look at what we will be learning via this Index.

What is List Comprehension?

List comprehensions are tools that allow us to transform one list into another list. This transformation changes the way code will be interpreted and makes it easier. We can include elements conditionally in the list and these elements can be transformed as per the need. The multiline statements can be avoided with the usage of the list comprehensions. 

A list comprehension Python is enclosed in the square brackets. They always include the keywords, ‘for’ and ‘in’. They are important tools to learn if you are looking for faster programming. Next, we will see how we can do that and how the List comprehensions provide the time advantage.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

The Time Advantage

List comprehensions save a lot of time by writing the code in a different manner like multiple statements into single lines. The only thing you have to look for is where you can use the list comprehension.

Here, let’s look at few examples.

Now this code using ‘for’ loop is written in multiple statements as you can see.

new_things = []
for ITEM in old_things:
    if condition_based_on(ITEM):
        new_things.append("something with " + ITEM)

The above code can be written in the following way using the list comprehensions.

new_things = ["something with " + ITEM for ITEM in old_things if condition_based_on(ITEM)]

Both codes are describing the same thing.

This is why the list comprehensions are very handy if you understand them. Here is another example:

mylist = ['ABC', 'fOO', 'bar']
tmp = []
for x in mylist :
mylist = tmp

In this example, we have a list of strings. We require the strings which are in lowercase. However, we can see according to the python standards that it’s a lot of code for a really simple task. Now, we will use list comprehensions to write this code in an easier manner.

mylist = [x.lower() for x in mylist]

It saves a lot of time as we can witness the above code. So, is it convenient to use List comprehensions in Python? This is because list comprehensions in python are optimized for the python interpreter to spot the predictable pattern while looping, henceforth making the code’s execution faster.

[Related Page: Python Lists with Examples]

Generator Comprehension/Expressions

Generator expressions are similar like List comprehensions. However, in contrast to the list comprehensions, they focus on one by one execution of the items. In other words, as we know, list comprehension takes up all the required items and packs them up in the list whereas generator expression focuses upon generating one item at a time rather than stacking them into one. Instead of brackets, parentheses are used in the Generator Expressions.

Since only one item is being processed by the Generator expressions there is less usage of the memory. Generator expressions are useful when one has to deal with items that require a lot of work and calculation, whereas, in case if one needs all the values, then he/she should proceed with list compressions. Here is an example:

>>> my_list = [1, 3, 5, 9, 2, 6]
>>> filtered_list = [item for item in my_list if item > 3]
>>> print filtered_list
[5, 9, 6]
>>> len(filtered_list)
>>> # compare to generator expression
>>> filtered_gen = (item for item in my_list if item > 3)
>>> print filtered_gen  # notice it's a generator object
>>> len(filtered_gen) # So technically, it has no length
Traceback (most recent call last):
  File "", line 1, in 
TypeError: object of type 'generator' has no len()
>>> # We extract each item out individually. We'll do it manually first.
>>> filtered_gen.__next__( )
>>> filtered_gen.__next__( )
>>> filtered_gen.__next__( )
>>> filtered_gen.__next__( ) # Should be all out of items and give an error
Traceback (most recent call last):
  File "", line 1, in 
>>> # Yup, the generator is spent. No values for you!
>>> # Let's prove it gives the same results as our list comprehension
>>> filtered_gen = (item for item in my_list if item > 3)
>>> gen_to_list = list(filtered_gen)
>>> print gen_to_list
[5, 9, 6]
>>> filtered_list == gen_to_list

In the code given above, we can see that all items are being handled one at a time.

Check Out Python Tutorials

Why use Generator in Python?

You might have heard about iterators. If not, here is their description:
Iterators are objects which we can iterate or loop. Strings, lists, and dictionaries are few objects that we can iterate. Generators are helpful in marking any function as iterators.

Now, for understanding the use of Generators, we must in turn investigate iterators. Iterators perform what programmers remark “Lazy Evolution”.

Iterators compute value of any item only when asked. In other words, iterators don’t compute the value of each item soon after initialization. Iterators are useful when there is large amount of data required to be computed. Hence, we can use the data as per requirement.

Now that we are familiar with iterators, we will understand the importance of Generators.

Generators are special kind of functions. They use “yield” statement to return data. Since iterators are a very important aspect of coding in Python, they are bound to be error free. Generators facilitate this as they are helpful  in tracking each element due to their step by step usage. Hence, the generators must be efficiently used in the program.

Often, there are a lot of possible codes whose output may be infinite possible values. Since we very well know that we can’t store infinite values due to hardware and memory limitations, generators are quite useful in this case as they handle one item at a time.

[Related Page: Introduction to Python Programming]

List Comprehension vs. ‘for’ Loop in Python

As we already know that List comprehensions provide easier, more convenient and time saving way of coding in Python, we will now see how much useful they are in contrast to ‘for’ loops.
We will understand it with the help of an example:

Here we will separate the letters of the word ‘earth’ using ‘for’ loop.

h_letters = []
for letter in 'earth':

The output of this program will be:

['e', 'a', 'r', 't', 'h']

You can see that it is somewhat longer program for such an easy task. List comprehensions make this process easier.

Now, we will write the above program using list comprehensions:

h_letters = [ letter for letter in 'earth' ]
print( h_letters)

Again, the output will be:

['e', 'a', 'r', 't', 'h']

The variable h_letters in the above code is assigned with a new list. The list contains the item to be iterated i.e. string ‘earth’. 

It is clear from this program that the list comprehension provides a much easier way to write a code wherever applicable.

The major feature of the list comprehension is its ability to handle strings of tuple and handling it as a list. Whereas in the ‘for’ loops, Python assigns a name to each element of the iterable item and then executes as it names them.

Please note that not all loops can be written using list comprehension.

Frequency Asked Python Interview Questions

List Comprehensions vs. Lambda functions

Apart from list comprehensions, Python also provides other built-in functions to handle the code in lesser lines. One such function is Lambda function.

Now, we will take the same code with what we used for List comprehension vs. ‘for’ loop.

Now the program for list comprehension:

h_letters = [ letter for letter in 'earth' ]
print( h_letters)

And the output:

['e', 'a', 'r', 't', 'h']

Now, with the Lambda function:

letters = list(map(lambda x: x, 'earth'))

Again the output is:

['e', 'a', 'r', 't', 'h']

But, still, List comprehension is better than the lambda function. With lambda function, you have to use the ‘map’ function. Lambda function is used for simple expression. In that case, ‘map’ usage results in function call for each element, whereas the list comprehension compiles it into a single loop that builds the list. 

Even in cases of existing functions, list comprehension seems more usable as it keeps the code more readable than lambda function. So, most programmers prefer list comprehension but usage of the lambda function still depends upon personal choice.

Nested Loops in List Comprehension

For multiple iterations, one can use nested loops. The loops then can be flattened with the use of the List comprehension. This reduces the mishandling of the number of loops, especially if you are not that much familiar with Python. 

For understanding nested loops, first we take an already existing ‘for’ loop in consideration and then use it for list comprehension with multiple conditions. Then, using this code, we will get a new list which iterates 2 lists and then finally use it for mathematical operations, hence, nesting both lists. Here is the nested code block of ‘for’ loop:

my_list = []
for x in [10, 20, 30]:
    for y in [1, 3, 2]:
        my_list.append(x * y)

In this code, we are multiplying items in the first list by the item in the second list, for each iteration.

The output of the following code will be:

[10, 30, 20, 20, 60, 40, 30, 90, 60]

Now, we will transform this into list comprehension. For that, we will condense each line of code into the single line. We will first assign each list to a variable using outer and inner parts of the ‘for’ loop and then execute the program. Then a print( ) statement will be used to receive the result.

Here is the nested loop for the code:

my_list = [x * y for x in [10, 20, 30] for y in [1, 3, 2]]

In this code the first list [10, 20, 30] is being assigned to the variable ‘x’ whereas the second list [1, 3, 2] is being assigned to the variable ‘y’ using ‘for’ loop. Then x * y determines the multiplication of both lists.

List comprehension flattens the both nested ‘for’ loops in the single line while creating the exact list which is to be assigned to the ‘my_list’ variable.

This clearly determines the usefulness of the list comprehension in which even multiple loops are flattened in the single line. However, the thing that should be kept in mind is that, in case, if we have large number of loops to be nested and readability of the code is important, then breaking up the code in loops will be a better choice.

Conditionals in List Comprehension

List comprehension is compatible with using more conditional statements for modifying sequential data types or existing lists, while creating new lists. This can help in getting a more conditional and specific result when one has many lists to handle and output is required to be refined.

Here is an example of ‘if’ statement used in list comprehension:

man_tuple = ('Venom', 'Ironman', 'Batman', 'Spiderman')
man_list = [man for man in man_tuple if man != 'Venom']

Here, in this code, the list comprehension uses tuple  ‘man_tuple’ as basis for the new list called ‘man list’ . Now, apart from the ‘for’ and ‘in’ keywords, the ‘if’ statement is being used to add those items to the output, which is not equivalent to the string ‘Venom’, hence the ‘if’ statement takes those items from the tuple that do not match ‘Venom’.

Now, in the output, we will see that ‘man’ list contains all the items as ‘man tuple’ except the ‘Venom’, which has been omitted.

['Ironman', 'Batman', 'Spiderman']

Now, we will see a more complex example that consists of mathematical operators, range ( ) sequence, and the integer type.

number_list = [x ** 3 for x in range(10) if x % 2 == 0]

In this code, the list ‘number_list’, will contain cubed value of each item which is present in the range of 0-9, if the item’s value in this case is divisible by 2. Here is the output of the code:

[0, 8, 64, 216, 512]

The list comprehension is functioning more by limiting the number of items that will form the new list from the existing sequence.

Key points to remember

  • List comprehension is a major tool for the programmers looking to excel in Python. It provides abridged syntax, helping in efficient completion of the code, while limiting the span of the code, making it easy to read. 
  • Though it should be kept in mind that the long lines of code can become confusing and seriously hurt the readability of the program, the use of list comprehensions must be done cleverly.
  • Since the list comprehension is based upon the set builder notation in mathematical form, it is quite easier to be used in the coding process. It should be used for the matrices-related codes.
  • All list comprehensions can be written in the form of loops. However, not all loops can be transformed into list comprehensions. This is the most important thing considering if you are too much dependent upon the list comprehensions, then there is a possibility for very complex codes, in which you cannot use the long lines of codes.
  • In complex codes, you will be required to use loops. So be sure about brushing up the loops, otherwise, now or then, you may run into any problem.
Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings. Protection Status