Table of Contents
Introduction to List comprehension in Python
List comprehension is a pythonic way of expressing a for loop
that appends to a list in a single line of code. It is much faster than for loop and map() function. Most python programmers prefer to use list comprehension rather than for loop or any other method. In this tutorial we will cover some of the advanced topics related to list comprehension. We will learn the syntax, difference between loop and list comprehension and its advantages. Moreover, we explain some of the common mistakes that programmers make while using python list comprehensions and will discuss a few disadvantages as well.
List comprehension Syntax in Python
A list comprehension had three main components:
- Output: which can be string, number, list or any object you want to put in the list
- for statement
- condition: This is an optional statement
The simple syntax of Python list comprehension looks like this.
[[output value] for(statement) if (conditions)]
However, this formatting is not the only way.
There can be many more possibilities to use them in different ways. For example, there can be logics that can have multiple ‘for-statements’ and ‘if conditions’ and they can change positions as well. The only thing that does not change however is the position of the output value, which always comes at the beginning.
Create list using Python list comprehension
Let us now take an example to create a list. First we will use a simple for loop to create a python list and then will create the same list using list comprehension. These examples will show you the difference between for loop and list comprehension.
If we want to create a list of even numbers from 1 to 20 using a for loop, the syntax will look like this.
# created an empty list
evenList = []
# simple for loop.
for i in range(1, 20):
# checks condition for even
if i%2==0:
evenList.append(i)
# printing list
print(evenList)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
Now let us create the same list using the list comprehension method.
# using list comprehension
evenlist = [i for i in range(1, 20) if(i%2==0)]
# printing
print(evenlist)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
Multiple for statements in Python list comprehension
We can use as many for loops as we want in list comprehensions. Here we will see how we can use them. First let us take a nested for loop and then will give the same output using list comprehension.
Here is an example of a nested for loop which takes each element from one list and multiplies it with the elements of the second list and stores the result in the third list. See the example below.
# list of numbers
list1 = [1, 2, 3 ]
list2 = [1, 2, 3 ]
# creating empty list for out put
result = []
# using nested for loop
for i in list1:
for k in list2:
result.append(i*k)
# printing
print(result)
Output:
[1, 2, 3, 2, 4, 6, 3, 6, 9]
Now let us use list comprehension to have the same output.
# list of numbers
list1 = [1, 2, 3 ]
list2 = [1, 2, 3 ]
# using list comprehension
result = [i*k for i in list1 for k in list2]
# printing
print(result)
Output:
[1, 2, 3, 2, 4, 6, 3, 6, 9]
You can see how the list comprehension solves complex and long problems in one code allowing us to use multiple for statements in one line.
Multiple conditional statements in Python list comprehension
We can use multiple conditional statements in list comprehension in python. First let us take an example of nested if statements using a simple for loop and then we will try to implement the same logic using list comprehension.
Example-1: Use Nested if statements in List Comprehension
See the example which uses nested if statements and give output of all positive even numbers from a given list.
# list of numbers
list1 = [1, 2, 3 , -5, 8, -10, 4, -4, 6]
# creating empty for result
result =[]
# using for loop
for i in list1:
if i >0:
if i%2==0:
result.append(i)
# printing
print(result)
Output:
[2, 8, 4, 6]
Now let us implement the same logic using list comprehension in Python.
# list of numbers
list1 = [1, 2, 3 , -5, 8, -10, 4, -4, 6]
# using list comprehension with multiple conditional statement
result = [i for i in list1 if i>0 if i%2==0]
# # printing
print(result)
Output:
[2, 8, 4, 6]
Example-2: Use Nested for loops in List Comprehension
Now let go one more step ahead and use multiple for statements and multiple conditional statements in list comprehension. Again we will first implement the logic using nested for loop and nested if statement.
See the following example which loops over two given lists and prints out the result by multiplying only even positive numbers from each list.
# list of numbers
list1 = [1, -5, 8, -10, 4, -4, 6]
list2 = [2, 3, 4, -9, -5, 2, 9]
# empty list
result = []
# nested for loop
for i in list1:
for k in list2:
# using nested if statements
if i >0 and i%2==0:
if k> 0 and k%2==0:
result.append(i*k)
# printing
print(result)
Output:
[16, 32, 16, 8, 16, 8, 12, 24, 12]
Now let us implement the same logic using list comprehension and see how we can avoid using multiple lines and implement the logic in only one sentence.
# list of numbers
list1 = [1, -5, 8, -10, 4, -4, 6]
list2 = [2, 3, 4, -9, -5, 2, 9]
# list comprehension w
result = [i*k for i in list1 for k in list2 if i>0 and i%2==0 if k > 0 and k%2==0]
# printing
print(result)
Output:
[16, 32, 16, 8, 16, 8, 12, 24, 12]
Dictionary comprehension in Python
Dictionary comprehension in Python is similar to list comprehension with a few changes in syntax. It uses curly brackets {}
instead of square blacked []
and uses extra colon as well.
Let first create a dictionary using a for loop and then we will create a dictionary using dictionary comprehension so that the difference between dictionary and list comprehension will be clear.
See example which creates a dictionary using a simple for loop.
# new dictionary
newDic = {}
# using for loop
for n in range(1, 10):
if n%2==0:
newDic[n] = n**2
# printing dic
print(newDic)
Output:
{2: 4, 4: 16, 6: 36, 8: 64}
Now let us implement the same logic using dictionary comprehension.
# Use dictionary comprehension
newDic = {n:n**2 for n in range(1,10) if n%2 == 0}
# printing
print(newDic)
Output:
{2: 4, 4: 16, 6: 36, 8: 64}
Python list comprehension and generator expressions
A Generator Expression is doing basically the same thing as a List Comprehension with a small difference. As we have seen that a list comprehension in python executes immediately and returns a list. While generator expressions return an object that can be iterated. There is a little difference between the syntax as well. For example, list comprehension is enclosed in square brackets []
while the Generator expression is enclosed in plain parentheses ()
.
Let's see the differences through an example.
# creating list comprehension
lC = [i for i in range(10)]
# creating generator expression
GE = (i for i in range(10))
# printing
print(lC)
print(GE)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<generator object <genexpr> at 0x7fd636717970>
You can see that the output of generator expression is an object. We can iterate over it using a loop. let us now iterate over generator object to print the elements.
# creating list comprehension
lC = [i for i in range(10)]
# creating generator expression
GE = (i for i in range(10))
# printing
print(lC)
# iterating GE
for i in GE:
print(i)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
1
2
3
4
5
6
7
8
9
Now let us check which of the two takes more size. We can check it by using python’s built-in function called getsizeof()
. See the example below:
# importing module
import sys
# creating list comprehension
lC = [i for i in range(10)]
# creating generator expression
GE = (i for i in range(10))
# printing
print(sys.getsizeof(lC))
print(sys.getsizeof(GE))
Output:
184
112
Moreover, we cannot access the elements using indexing in generator expressions as we do in list comprehension. If we try to access elements using indexing, python will give us an error. See example below:
# creating list comprehension
lC = [i for i in range(10)]
# creating generator expression
GE = (i for i in range(10))
# printing
print(lC[2])
print(GE(3))
Output:
As you can see we get an error because the generator object is not callable.
Advantages of using python list comprehension
There are many advantages of using list comprehension.
One of the biggest advantages of list comprehension is that they allow developers to write less code that is often easier to understand. Usually the complex problems and multiple lines of code can be written in only one line using list comprehension. Moreover, it is usually faster, but only if it is actually being used to create a new list. If you notice, in the examples above, we included the append method in the for loop, but we didn't use the append method in the list comprehension. This is because in Python, we don’t need to load the append attribute off of the list and call it as a function. Instead, in a comprehension, a specialized list append bytecode is generated for a fast append onto the result list.
Common mistakes with Python list comprehension
Because python list comprehensions are powerful and strong, python developers and new python programmers mostly use them to make their code shorter and clearer. However, there might be some common mistakes that most of the new python programmers make while using them.
Things become a little complicated when using multiple nested for loops along with conditions. Sometimes, new python developers use the for loops or the conditions in reverse order. Although this will not give us syntax error but will not provide us with the desired output. See the following example which will make it clear, how to use multiple loops with multiple conditions.
Let say we have the following code of for loop:
# creating list
list1 = []
# for loop
for i in range(1, 5):
if i%2==0: # first inner condition
for j in range(1, 10): # second for loop
if j%4==0: # second inner condition
list1.append(i - j) #appending result
print(list1)
Output:
[-2, -6, 0, -4]
The following is syntactically correct but will not gives us the same output:
# wrong way of implementation
list1 = ([i - j for i in range(1, 10) if i%4==0 for j in range(1,5) if j%2==0])
# printing
print(list1)
Output:
[2, 0, 6, 4]
Although python didn't give us an error because the syntax is correct but the arrangement of for loop is incorrect.
The following is a correct way of implementing the same logic.
# correct way of implementation
list1 = ([i - j for i in range(1, 5) if i%2==0 for j in range(1,10) if j%4==0])
# printing
print(list1)
Output:
[-2, -6, 0, -4]
So the correct way is to go from out for loop to inner along with outer most conditions to inner.
However, we can also write the inner for loop before the first condition and then can write all the conditions at the end. But again the order should be followed. See the example below.
# correct way of implementation
list1 = ([i - j for i in range(1, 5) for j in range(1,10) if i%2==0 if j%4==0])
# printing
print(list1)
Output:
[-2, -6, 0, -4]
Summary
List comprehension is a simple way to create lists in a more readable and concise manner in one line. They make our code shorter and understandable. In this tutorial we learned about python list comprehensions, their syntax and uses. We also cover different formatting, multiple for loop and multiple conditions used in python list comprehension. Moreover, we discussed some of the advantages and the mistakes that most people make while using them.
Further Reading
list comprehension
More about Python List Comprehension
Python list comprehension syntax