Comprehensions in Python: What are they? Why Should you use them?

Kieron Spearing
Geek Culture
Published in
4 min readJan 26, 2022

--

Being one of the most iconic and Pythonic syntax in the language, I am certain everybody has heard of or worked with list comprehensions already.

But did you know that there are three other types of comprehensions in the language?

Throughout this post, I will talk about what comprehensions in Python are and why one would use them. I will also go through when using a comprehension can be detrimental to the readability of code.

What is a comprehension in Python?

Essentially a comprehension in Python is the syntax used to create a new sequence by performing an operation over another iterable.

There are four types of comprehensions in Python.

  1. List Comprehensions
  2. Dict Comprehensions
  3. Set Comprehensions
  4. Generator Comprehensions

A common misconception about comprehensions that most beginners have is that the initial iterable needs to be of the same type as the comprehension. However, this is not the case, for example, if you are working with a dict comprehension the original iterable can also be a list.

The type referred to in the name is the type of sequence that is returned from the comprehension.

Why should one use a comprehension in Python?

We use comprehensions in our code because it is more Pythonic and readable to use them. However, one needs to be careful when using them in certain scenarios as there are times when they can have the opposite effect.

List Comprehension

A List comprehension is the most common type of comprehension used in Python, and it follows the simple syntax:

As we can see here the condition is completely optional, but if you use it depending on whether you are using the else clause the position of it in the list comprehension will change.

We can also see that they are extremely easy to use and can be quite handy in reducing the use of for loops, making the code cleaner.

The best way to get the hang of it is to play with them yourself and learn their limitations. Below is a real example of the usage to give you an idea.

Dict Comprehension

As I mentioned earlier the type mentioned in the comprehension name refers to the type of sequence that it creates, not the type of iterable used in the comprehension.

The syntax for a dictionary comprehension is:

Once more the condition is optional here.

You may use this in many different scenarios, for example, imagine you want to be able to compare the salaries earned based on the position of an employee in the company, and you have a long list of dictionaries containing all sorts of information about each employee.

The truth is you may be able to work with it but there is a lot more information than needed if you only care about the position and salary, thus we could use a dictionary comprehension to easily create a new dictionary with only the information we want.

As you can see above we can achieve this very easily and cleanly in one line using a dict comprehension.

Set Comprehension

A set comprehension functions similarly to a list comprehension, except it has all the benefits of a set, namely items in the set cannot be repeated and the values are not in order.

It also uses a similar syntax to a list comprehension except it uses curly brackets instead of square brackets.

There are times in which this can be useful but you must remember to be careful as the beneficial aspects of the set can cause issues if requiring the results to be ordered, as can be seen below:

Generator Comprehensions

Generator comprehensions are the least known type of comprehension that we have in Python.

A generator is a function that returns an object(iterator) which we can iterate over one at a time, it is a unique syntax in python that is usually generated from a function with the yield keyword.

The syntax of a generator comprehension is as simple as all the others:

The beauty of using a generator is that we can iterate over it simply by using the built-in function next() to iterate over it one step at a time.

When shouldn’t one use comprehensions?

After reading everything here it seems like a no-brainer to always use comprehensions, correct?

Well no.

Comprehensions in python can be used in all the ways above to create readable code but when we use nested comprehensions we need to be careful as it can cause the code to become less readable.

Nested Comprehension

A nested comprehension is a term we use to refer to comprehensions being done within other comprehensions.

While this is an extremely powerful feature, it can also create the opposite effect in form of readability.

For example, both the below scripts create the pyramid below.

#
##
###
####
#####
######
#######

As we can see, while this is a fairly basic example it is already more readable without the nested comprehension and as the complexity increases, the harder it will be to maintain readability with these.

Having been through the article and understanding the comprehensions we can not fully understand the power they bring to the language, but at the same time, we need to be careful not to abuse them as it can quickly become detrimental to the readability when the logic is not simple.

Whenever I start to nest comprehensions myself I always like to remember the simple saying:

The code should explain the comments, not the comments explaining the code.

And then decide whether it may be best to write the logic out without the comprehension.

The beauty in using comprehensions is that it helps you to produce efficient readable code when used correctly.

I hope you enjoy the reading and keep learning!

--

--

Kieron Spearing
Geek Culture

I am a Full-Stack Developer at StyleSage and a Food enthusiast with 2 years experience in technology and 7 years experience working in Michelin Star restaurants