To the point: Python List Comprehension

List comprehension is a powerful tool in python. It brings simplicity and makes your code easier to read. Misused also can be a nightmare. Learn how and when to use it.

Mathias Santos de Brito
3 min readNov 8, 2020
Photo by Chris Ried unsplash.com/@cdr6934— Extracted from Usplash
Photo by Chris Ried at https://unsplash.com/@cdr6934

List comprehensions are shortcuts for composing lists out of an iterable. The most simple and useless example we can have is using it to create a list containing the same elements of an input list, like:

Ok, I know, this piece of code was useless, however, it is very good to understand the mechanics of list comprehensions. The first x that appears inside the square brackets can be any valid Python expression. The result of this expression will go into the new list. Let’s see:

As you can see, we extracted from the input list a new list containing a sequence containing the squares of the numbers. The code is clean and when the concept of list comprehensions is well packed in your head, it is super intuitive and it is not a complex concept to get.

In the above example we get 2 powered by each element of the list, for the newcomers in python and programming, remember that calling a function is a valid expression. With this example, it is clear that the possibilities are many.

Let’s see an example using a list of strings:

We can test the value before deciding if it should go into the list being
crafted, for that we can use an if statement creating some sort of filter. So, we can do:

We can also have else statements, note that the if clause now appears before
the for clause now:

Nice huh! despite this example still clean and easy to read, from here on, if you do not take care things can become complicated, the code messy and hard to understand/read. Be careful with complex and long if statements especially if you are using PEP-8 that limits the size of a line to 80 columns. As a rule, I would say, if you are splitting a list comprehension through multiple lines, forget it and use a regular for, for the sake of clarity.

Indeed the syntax of list comprehensions allow you to have even nested Fors,
it is described in the Python documentation as:

A list comprehension consists of 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.

Let’s analyse the following, that is based on an example found in the Python
documentation, however, I intentionally modified the expected output:

But why did I force this error? The assertion will complain, I did this on purpose since some will not realize that the two fors are nested. They are not running concurrently, they are nested. The expected result does not come from the simple analysis of elements with the same indexes in the two lists. Indeed this comprehension is equivalent to:

So, each value in the outer for loop will be compared to all the elements of
the second list being iterated in the inner for loop.

This last comprehension example is a good example of how things can get
complicated quickly. So my advice is, use it if you can keep it simple. Do not
abuse nested fors and avoid complex if clauses with complex logic.

You can write code without list comprehension it is totally ok, and you can
still have clean code, obviously. However the syntax of comprehensions is
intuitive and whenever you get the concept, it is extremely easy to read. It is
expected from python developers that they understand this concept since it will appear quite often. However do not abuse it as I already mentioned, if it is
starting to become complicated to understand/ready leave it.

I hope you enjoyed this article. If you have any critic or suggestions
let me know in the comments.

Note: To the point intends to be a series of articles that try to bring programming concepts in a very practical, pragmatic, and programmatic approach. This is the first article of the series. Did my best with my English! :)

--

--

Mathias Santos de Brito

Cmp. Sci. Professor at UESC/Brazil, PhD Student at TU Berlin. Felt in love for programming at 12yo, was Clipper. Lot changed since then but the love got bigger.