List Comprehensions in Python

Jessica YungProgrammingLeave a Comment

1. What are list comprehensions?

List comprehensions construct lists in natural-to-express ways. They can replace map-filter combinations and many for loops.

They’re just syntactic sugar. That means they make your code easier to read (and prettier).

Example 1: For loops -> List comprehension

Example 2: Map-filter -> List Comprehension

(I will do a post on map and filter and link to it here later.)

2. Why might we use list comprehensions?

Because they’re more elegant (see above). That’s basically it.

The idea came from mathematics, where you may have seen sets constructed like this:

S = \{ x^2 : x \text{ in } {1,...,10} \}

(Read: S is the set of all x^2 such that x is a positive integer between 1 and 10 inclusive.)

M = \{ x | x \text{ in }  \text{ S and } x \text{ odd} \}

(Read: M is the set of x such that x is in S and x is even.)

Notice the parallels with the corresponding list comprehensions:

3. Understanding list comprehensions

Trey Hunner has an excellent animated, step-by-step tutorial on going from for loops to list comprehensions (and vice versa).

The way to get familiar with list comprehensions once you’ve understood what they are is through practice: attempting relevant problems and constantly asking yourself if you can replace your for loops or map-filter combos with list comprehensions. Sites like HackerRank or CodeWars are great for finding problems to practice on.

4. Techniques

Technique 1: Nested loops

You might have a for loop that looks like this:

The corresponding list comprehension is

Is this what you expected? The order in which you list the clauses is the order in which you’d write the for loops if they were for loops, and not how you’d say them in plain English (which would be the other way round).

Let’s rewrite the list comprehension with line breaks in between to make it clearer:

Example 3: Primes under 100

Here is an interesting example where we use list comprehensions to get a list of all prime numbers smaller than 100.

(Example taken from Python Course)

Technique 2: zip(): dealing with two or more elements at a time.

5. Tip: Improving Readability

It’s important that your code is easily understandable by other people (and by yourself should you have to revisit it months later). List comprehensions make code more concise, but you can imagine that if you have many conditions or nested for loops, they can get messy fast. There are two points to note here:

1. If there are two many for or if clauses, split the list comprehension up into multiple list comprehensions.

2. If your list comprehension is long, insert line break between different clauses. (See first_names, last_names example, latter no_primes example.)

6. Related concepts

Dict comprehensions and set comprehensions (available in Python 3) are similar but are applied to dictionaries and sets.

Application: In our example constructing lists of prime numbers above, the list noprime contains duplicates. So if we wanted to remove the duplicates we could create a set instead.

7. Further reading:

Leave a Reply