Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 149 ยท Data Structures

List Comprehensions

Write Python list comprehensions with confidence. Transform and filter data in one line, use if/else inside them, flatten nested lists and know when a plain loop reads better.

A huge share of everyday code follows one pattern: start with an empty list, loop over some data, maybe skip a few items, and append a transformed version of each remaining item. You've written it already in for Loops. It works, but it takes four lines to express one idea: "make a list of this for each item in that".

Python has a dedicated syntax for that idea, the list comprehension. It builds a new list in a single expression that reads almost like the sentence describing it. Comprehensions are one of the most recognizably "Pythonic" features of the language. In this lesson you'll learn their syntax, add filters and conditional values, nest them, and, just as importantly, learn when not to use them.

From loop to comprehension

Here is the same result built two ways:

Python Loop vs comprehension

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The general shape is:

Python
[expression for item in iterable]
  • for item in iterable is the same as the header of a for loop.
  • expression is what goes into the new list for each item. It usually uses item, but it can be any expression.
  • The square brackets say "collect the results in a new list".

Read it from the middle out: "for each price in prices, compute round(price * 1.2, 2), and put the results in a list." The original list is never changed.

Python Transforming data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Filtering with if

Add an if clause at the end to keep only the items that pass a test:

Python
[expression for item in iterable if condition]
Python Filtering items

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The condition is checked for every item, and only items where it's truthy reach the expression. The last example relies on truthiness: w.strip() is an empty (falsy) string for blank entries.

You may chain several if clauses; they act like and:

Python
[n for n in range(100) if n % 3 == 0 if n % 5 == 0]
# same as: if n % 3 == 0 and n % 5 == 0

Most people find a single if with and clearer.

Choosing a value with if/else

Sometimes you don't want to drop items, but to pick between two values for each one. That's a job for a conditional expression, a if condition else b, and it goes in the expression position, before the for:

Python if/else inside the expression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The position tells you the meaning, and mixing them up is the most common comprehension error:

Syntax Meaning Output length
[x for x in data if cond] Keep only items that pass Can be shorter
[a if cond else b for x in data] Choose a value for every item Always the same

[x for x in data if cond else 0] is a SyntaxError, because a filter can't have an else.

The labels example chains two conditional expressions. It works, but it's at the edge of readable. If it grew any further, a small function like describe(t) used as [describe(t) for t in temps] would be clearer.

Nested comprehensions

A comprehension can have more than one for. The clauses are read left to right, in the same order as nested loops:

Python Flattening and combining

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A different kind of nesting puts a comprehension inside the expression of another. That builds a nested list, such as a grid:

Python Building and transforming grids

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is the safe way to create a grid that the Lists lesson warned about: the inner comprehension runs again for every row, so each row is a separate list.

Comprehensions are their own little scope

The loop variable inside a comprehension doesn't leak out. After [x * 2 for x in data], there's no x variable left behind (unlike a regular for loop). The comprehension can still read variables from the surrounding code:

Python
rate = 1.2
x = "unchanged"
converted = [x * rate for x in [10, 20]]
print(x)            # still "unchanged"

When not to use a comprehension

Comprehensions are for building a list. They become a liability when they're used for something else, or when they get too dense.

  • Don't use them for side effects. [print(n) for n in data] prints the values, but also builds a list of Nones that's thrown away. Use a plain for loop when the point is to do something.
  • Keep them short. One for and one condition is ideal. Two for clauses are fine for flattening. Beyond that, write a loop or move the logic into a function.
  • You don't always need the list. If you only feed the results into sum(), max(), any() or all(), drop the brackets and use a generator expression, which produces items one at a time without storing them: sum(p * 1.2 for p in prices). You'll learn more in Dictionary and Set Comprehensions.
  • A built-in may already exist. list(data) copies, sorted(data) sorts, and [x for x in data] is just a slower list(data).

Tip: A comprehension is the right choice when you can describe it in one sentence: "the squares of the even numbers", "the names in uppercase", "the non-empty lines". If the sentence needs "and then" or "unless", use a loop.

A small practical example

Comprehensions shine in data cleanup. Here a messy block of text becomes structured data in a few readable steps:

Python Parsing a small report

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each line does one job, and each reads as a sentence. Notice the unpacking in for name, price, qty in rows: each row has three parts, so it unpacks into three variables, exactly as in a regular for loop.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does [n * 10 for n in range(4)] produce?

    range(4) gives 0, 1, 2 and 3, and each is multiplied by 10.
  2. 2. Which comprehension keeps only the words longer than 3 characters?

    A filter goes at the end. Option 1 is a syntax error (if without else in the expression), and option 2 builds a list of True/False values.
  3. 3. What is [x if x > 0 else 0 for x in [-2, 5, -1, 3]]?

    The if/else sits in the expression, so every item produces a value - negatives become 0 and the list keeps its length.
  4. 4. What does [a + b for a in "xy" for b in "12"] produce?

    The for clauses nest left to right like loops, so for each a the inner loop runs over every b.
  5. 5. Why is [print(n) for n in data] considered bad style?

    Comprehensions are for building lists. When you only want the side effect, a plain for loop is clearer and wastes no memory.

Summary

  • A list comprehension [expression for item in iterable] builds a new list in one expression; the source is never changed.
  • Add if condition at the end to filter items; the result can be shorter than the input.
  • Put a if condition else b in the expression to choose a value for every item; the result keeps the same length.
  • Multiple for clauses nest left to right like loops; a comprehension inside the expression builds nested lists such as grids.
  • The loop variable stays inside the comprehension and doesn't leak out.
  • Use comprehensions to build lists, not for side effects; keep them short, and use a generator expression when you only need to sum or test the values.

Next: Tuples, Python's immutable sequences, along with packing, unpacking and named tuples.