# Python List Comprehension: What it Is, How it Works, Examples

## Learn how to write polished and efficient Python code

In mathematics, there’s a concept called set-builder notation, also called set comprehension. Inspired by this principle, Python offers comprehensions, too. In fact, list comprehensions are one of the defining features of Python. They allow us to create concise, readable code that outperforms the uglier alternatives like `for` loops or using `map()`.

We’ll first look at the most well-known type: list comprehensions. Once we’ve got a good grasp of how they work, you’ll also learn about set comprehensions and dictionary comprehensions.

# What Are List Comprehensions?

A list comprehension is a language construct. It’s used to create a list based on an existing list. Sounds a little vague, I know, but after a few examples, that ‘ah-ha!’ moment will follow — trust me.

The basic syntax of a list comprehension is:

`[ <expression> for item in list if <conditional> ]`

The ‘`if`’-part is optional, as you’ll discover soon. However, we do need a list to start from. Or, more specifically, anything that is an iterator. We’ll use Python’s `range()` function, which is a special type of iterator called a generator: It generates a new number on each iteration.

# Examples of List Comprehensions

Enough theory, let’s look at the most basic example. I encourage you to fire up a Python REPL to try this yourself:

`>>> [x for x in range(1, 5)][1, 2, 3, 4]`

Some observations:

• The `expression` part is just `x`
• Instead of a list, we use the `range()` function. We can use `[1, 2, 3, 4]` too, but a `range()` is generally more efficient.

The result: a list of elements that we got from `range()`. Not very useful, but we did create our first list comprehension. We could just as well use:

`>>> list(range(1,5))[1, 2, 3, 4]`

So, let’s throw in that if-statement to make it more useful:

`>>> [x for x in range(1,10) if x % 2 == 0][2, 4, 6, 8]`

The `if`-part acts as a filter. If the condition after the `if` resolves to `true`, the item is included. If it resolves to `false`, it’s omitted. This way, we can get only the even numbers using a list comprehension.

So far, our `expression` (the `x`) has been really simple. Just to make this absolutely clear though, `expression` can be anything that is valid Python code and is considered an expression. Example:

`>>> [x + 4 for x in [10, 20]][14, 24]`

This expression adds `four` to `x`, which is still quite simple. But we could also have done something more complicated, like calling a function with `x` as the argument:

`def some_function(a):    return (a + 5) / 2    m = [some_function(i) for i in range(8)]print(m)# [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0]`

You mastered the basics — Congrats! Let’s continue with some more advanced examples.

## Nested list comprehensions

If `expression` can be any valid Python expression, it can also be another list comprehension. This can be useful when you want to create a matrix:

`>>> [[j for j in range(3)] for i in range(4)][[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]`

Or, if you want to flatten the previous matrix:

`>>> [value for sublist in [m for value in sublist]][0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]`

This is the same as above, but with some more whitespace to make this clearer:

`>>> [value     for sublist in m     for value in sublist][0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]`

The first part loops over the matrix `m`. The second part loops over the elements in each vector.

# Alternatives To List Comprehensions

The Python language could do without comprehensions; it would just not look as beautiful. Using functional programming functions like `map()` and `reduce()` can do everything a list comprehension can.

Another alternative is using loops. If you’re coming from a C-style language, like Java, you’ll be tempted to use `for`loops. Although it’s not the end of the world, you should know that list comprehensions are more performant, and they are considered a better coding style.

# Other Comprehensions

If there are list comprehensions, why not create dictionary comprehensions as well? Or what about set comprehensions? As you might expect, both exist.

## Set comprehensions

The syntax for a set comprehension is not much different. We just use accolades instead of square brackets:

`{ <expression> for item in list if <conditional> }`

For example:

`>>> {s for s in range(1,5) if s % 2}{1, 3}`

## Dictionary comprehensions

A dictionary requires a key and a value. Otherwise, it’s the same trick again:

`>>> {x: x**2 for x in (2, 4, 6)}{2: 4, 4: 16, 6: 36}`

The only difference is that we define both the key and value in the `expression `part.

# Conclusion

If you come from another language or if you are used to creating loops, try to replace some for loops with a comprehension, and see if it improves your code. It probably will!

Software developer by day, writer at night. Webmaster at https://python.land

### By Python Land

Get updated on our best articles. No spam. No selling of your data. Promised! Take a look.

Medium sent you an email at to complete your subscription.

## More from Python Land

Python articles for all types of programmers, from beginner to expert, from DevOps to Data Scientists

## Iteration J2: Authenticating Users

Get the Medium app