Python List Comprehension — Deep Dive (22/100 Days of Python)

Martin Mirakyan
5 min readJan 23, 2023
Day 22 of the “100 Days of Python” blog post series covering list comprehension

List comprehension is one of the most powerful and efficient features in Python. It allows developers to filter, modify, and perform other operations on lists in a concise and readable way. It is a great tool for working with lists, and it is widely used in the Python programming language. In this tutorial, we will explore the basics of list comprehension and provide several examples to help you understand how it works.

Filtering Data with List Comprehension

One of the main benefits of list comprehension is the ability to filter data. For example, let’s say you have a list of integers and you want to extract only the even numbers. Using a for loop, you would iterate through each number in the list and check if it is even. If it is, you would add it to a new list. With list comprehension, you can achieve the same result in a more concise and readable way, by applying the filter if number % 2 == 0 to each number in the list:

numbers = [19, 1, 12, 16, 8, 13, 10, 16, 15, 7]

# Using a for loop
even_numbers = []
for number in numbers:
if number % 2 == 0:
even_numbers.append(number)

# Using list comprehension
even_numbers = [number for number in numbers if number % 2 == 0]

In both of the implementations above the even_numbers list will have 12, 16, 8, 10, 16 numbers in it.

Filtering data with list comprehension can be used in a scenario where you have a dataset and you need to filter out specific elements based on certain criteria. For example, you might have a list of customer data and you want to filter out only the customers that live in a specific city.

Modifying Data with List Comprehension

Another use-case of list comprehension is modifying the data. For instance, you may have a list of numbers and you want to square each number. Using a for loop, you would iterate through each number in the list, square it and add it to a new list. With list comprehension, you can achieve the same result by applying the operation number ** 2 to each number in the list.

# Using a for loop
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = []
for number in numbers:
squared_numbers.append(number ** 2)

# Using list comprehension
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = [number ** 2 for number in numbers]

Both of these implementations result in the same final squared_numbers list but the second implementation is 3 times more concise (1 line instead of 3).

This can be used in a scenario where you have a list of items and you want to perform a certain operation on each of them. For example, you might have a list of prices and you want to calculate the final price after applying a discount of 10%.

Working with Nested Structures

List comprehension can also be used with nested loops. For example, you may have a matrix represented as a list of lists, and you want to extract all elements into a single list. Using for loops, you would iterate through each row of the matrix and then through each element of that row, adding each element to a new list. With list comprehension, you can achieve the same result by applying the operation [number for row in matrix for number in row]:

# Using for loops
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_matrix = []
for row in matrix:
for number in row:
flattened_matrix.append(number)

# Using list comprehension
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_matrix = [number for row in matrix for number in row]

Here the second example uses list comprehension with nested loops to achieve the same result. The flattened_matrix list is created by first iterating over all the rows of the matrix and then all the items in the row. So, the final result includes all the numbers from 1 to 9 in increasing order.

Applying a function to each element

You can also use list comprehension in combination with functions. For example, you may have a list of numbers and you want to apply a specific function to each number. Using a for loop, you would iterate through each number in the list, apply the function to it, and add the result to a new list. With list comprehension, you can achieve the same result by applying the function square(number) to each number in the list:

# Using a for loop
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def square(x):
return x**2


squared_numbers = []
for number in numbers:
squared_numbers.append(square(number))

# Using list comprehension
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = [square(number) for number in numbers]

The first example uses a for loop and a function square() to square each number in a list of integers. We start with an empty list called squared_numbers and iterate through each number in the numbers list. We append the square of the number (calculated by the square() function) to the squared_numbers list. The second example uses list comprehension to achieve the same result by applying the function square(number) to each number in the numbers list.

This can be used in a scenario where you have a list of elements and you want to perform a certain operation on each of them using a function. For example, you might have a list of strings and you want to convert them to uppercase using the upper() function.

Assigning Values Based on a Condition

List comprehension also allows you to use if-else statements. You may have a list of integers where you want to square each even number and leave the odd numbers unchanged. Using a for loop, you would iterate through each number in the list, check if it is even, square it if it is and add it to a new list, otherwise, you would add the number to the new list. With list comprehension, you can achieve the same result by having the conditional value at the beginning of the comprehension:

# Using for loop
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = []
for number in numbers:
if number % 2 == 0:
squared_numbers.append(number ** 2)
else:
squared_numbers.append(number)

# Using list comprehension
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = [number ** 2 if number % 2 == 0 else number for number in numbers]

Here the squared_numbers list is created by applying the operation number ** 2 if number % 2 == 0 else number to each number in the numbers list.

List comprehension is a very valuable feature in Python that allows developers to work with lists in a more efficient and readable way. It can be used to filter, modify, and perform other operations on lists, as well as to use nested loops, functions, and if-else statements.

What’s next?

--

--

Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)