Understanding List Comprehensions in Python

Żimuzo Obiechina
4 min readMay 3, 2021

--

Photo by Shahadat Rahman on Unsplash

Basic knowledge of Python data structures and Python operators required.

Starting out learning a new programming language, such as Python, can seem daunting at first, especially with technical terms like ‘list comprehensions’ coming up frequently. But when we deconstruct a concept into its components, we have a better chance of grasping its fundamentals and building on it.

In this article, you will learn what makes up a list comprehension, the various patterns of writing a list comprehension and the potential use cases for list comprehensions. But first, we will define and describe a list comprehension.

What is a list comprehension?

List’ and ‘comprehension’ are two separate terms in Python. A list is a data structure that contains an ordered sequence of values; on the other hand, a comprehension is a set of instructions on how a specific output should be created. Hence, a list comprehension is a set of instructions to create a new list.

The general structure of a list comprehension is as follows:

            new_list = [value for variable in iterable]

An iterable is any Python object that contains a set of elements. It allows a for loop operation to return each item in order, from the beginning of the object to the end. Examples of iterables are lists, tuples and sets.

The variable is the name assigned to each element in the iterable.

The value is the result of the for clause and / or if clause, for each element. Each value is then appended to the new_list in order.

As written in the Python docs, a list comprehension consists of square brackets containing an expression followed by a for clause, then zero or more for or if clauses.

The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

A list comprehension having a for clause means that it is a one-liner technique that can be derived directly from a for loop. For example, let us create a new list called numbers from an iterable range of values from 1 to 20, using a for loop:

With the for loop example above (and for loop operations in general), we are explicitly specifying each step of how we want to create the list. Now, we will rewrite the above as a basic list comprehension:

Notice that with a list comprehension, you only have to declare what you want the list to contain. Python decides how to create the list.

List comprehension patterns

In the example above, we learnt how to create a list comprehension from a simple for loop. Other patterns include:

  1. From a simple for loop with a conditional statement

In the example below, we want to create a list called numbers, containing only odd numbers, from a range of iterable values from 1 to 20.

As a list comprehension, it becomes:

2. From nested for loops

In the example below, we want to create a new list that combines elements from two separate lists. Each value in the new list should be a tuple that meets the condition in the conditional statement.

As a list comprehension, it becomes:

Notice how the order of the for clause and if statements are the same in both the nested for loop and list comprehension.

3. Nested list comprehensions

A list comprehension can contain another list comprehension. In the example below, we have a multidimensional list — a 3x3 matrix. It is a list containing three lists, each with 3 elements.

To spread this matrix out into a one-dimensional list, we can use a for loop as follows:

Using a nested list comprehension, it becomes:

Notice how using a for loop is more readable compared to the nested list comprehension? That is a tradeoff to using nested list comprehensions. It can get quite complex and difficult to read.

When working with multidimensional lists, it is generally recommended to be as explicit as possible. Use for loops to improve readability.

When to use list comprehensions

  • To create a new list. Although, there are alternative ways to create a list — using map or generator expressions, which may be more performant (depending on you and your team’s requirements), list comprehensions provide a succinct approach to creating a new list.
  • To optimize your code for readability. Since list comprehensions are one-liner statements, they can potentially enhance the reading experience of your codebase. Be careful not to overdo it.

Conclusion

As a beginner, being explicit with writing code comes naturally as you navigate learning the syntax of a programming language. But using techniques such as list comprehensions will give you that push into intermediate level programming. So go for it!

Additional resources:

  • Learn about the history of the introduction of list comprehensions into Python 2.0 here.
  • The same principles discussed here apply to set and dictionary comprehensions. Learn more about them here.

Thank you for reading. Let me know if you find this article helpful!

--

--

Żimuzo Obiechina
Żimuzo Obiechina

Written by Żimuzo Obiechina

Head in the clouds, hands on the keyboard

No responses yet