Chapter 41 of 54

Comprehensions in Python

By now you've written this pattern many times: create an empty list, loop over something, and append() a result each time.

squares = []
for n in range(1, 6):
    squares.append(n * n)

print(squares)

Output:

[1, 4, 9, 16, 25]

Python has a shorter, more readable way to write exactly the same thing:

squares = [n * n for n in range(1, 6)]

print(squares)

Output:

[1, 4, 9, 16, 25]

This is called a list comprehension. In this chapter, you'll learn list, set and dictionary comprehensions, and generator expressions, along with when to use them and when not to.

What is a Comprehension?

A comprehension is a compact expression that builds a new list, set or dictionary by looping over an iterable, optionally filtering and transforming each item.

Basic Syntax

[ expression   for item in iterable ]
     │               │
     │               └── the loop (same as a normal for loop)
     └── what to put in the new list for each item

Read it from the middle: "for each item in the iterable, put expression into the new list".

names = ["asha", "ravi", "neha"]

upper_names = [name.upper() for name in names]
lengths = [len(name) for name in names]

print(upper_names)
print(lengths)

Output:

['ASHA', 'RAVI', 'NEHA']
[4, 4, 4]

Filtering with if

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

[ expression   for item in iterable   if condition ]
nums = [12, 7, 3, 18, 25, 10]

evens = [n for n in nums if n % 2 == 0]
big_squares = [n * n for n in nums if n > 10]

print(evens)
print(big_squares)

Output:

[12, 18, 10]
[144, 324, 625]

The equivalent loop helps you see the order of things:

nums = [12, 7, 3, 18, 25, 10]

evens = []
for n in nums:            # 1. loop
    if n % 2 == 0:        # 2. filter
        evens.append(n)   # 3. expression

print(evens)

Output:

[12, 18, 10]

Transforming with if-else

Sometimes you don't want to skip items, you want to choose between two values for each one. For that, use a conditional expression (from the Conditional Expressions chapter) at the front:

[ value_if_true if condition else value_if_false   for item in iterable ]
marks = [45, 82, 30, 67]

results = ["Pass" if m >= 40 else "Fail" for m in marks]

print(results)

Output:

['Pass', 'Pass', 'Fail', 'Pass']

This is the part that confuses most beginners, so let's compare them side by side:

Goal

Where the if goes

Example

Output length

Filter (keep some items)

At the end, no else

[x for x in nums if x > 0]

Can be shorter

Transform (pick a value)

At the front, with else

[x if x > 0 else 0 for x in nums]

Always the same

Putting an else at the end is a syntax error:

nums = [1, -2, 3]

result = [x for x in nums if x > 0 else 0]

Output:

SyntaxError: invalid syntax

You can also use both together: transform at the front and filter at the end.

nums = [1, 2, 3, 4, 5, 6]

labels = ["even" if n % 2 == 0 else "odd" for n in nums if n > 3]

print(labels)

Output:

['even', 'odd', 'even']

Nested Loops in a Comprehension

You can write more than one for. They run in the same order as nested loops written normally: the first for is the outer loop.

pairs = [(color, size) for color in ["Red", "Blue"] for size in ["S", "L"]]

print(pairs)

Output:

[('Red', 'S'), ('Red', 'L'), ('Blue', 'S'), ('Blue', 'L')]

This is the same as:

pairs = []
for color in ["Red", "Blue"]:      # outer loop
    for size in ["S", "L"]:        # inner loop
        pairs.append((color, size))

Flattening a Matrix

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

flat = [value for row in matrix for value in row]

print(flat)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

A Comprehension Inside a Comprehension

To build a 2D list, put one comprehension inside another. The inner one builds each row:

table = [[row * col for col in range(1, 4)] for row in range(1, 4)]
print(table)

matrix = [[1, 2, 3], [4, 5, 6]]
transpose = [[row[i] for row in matrix] for i in range(3)]
print(transpose)

Output:

[[1, 2, 3], [2, 4, 6], [3, 6, 9]]
[[1, 4], [2, 5], [3, 6]]

This is also the correct way to create a grid of zeros. A common bug is writing [[0] * 3] * 3, which creates three references to the same row:

wrong = [[0] * 3] * 3
right = [[0] * 3 for _ in range(3)]

wrong[0][0] = 1
right[0][0] = 1

print(wrong)
print(right)

Output:

[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]

Set Comprehensions

Use curly braces to build a set. Duplicates disappear automatically:

words = ["apple", "Avocado", "banana", "blueberry", "cherry"]

first_letters = {w[0].lower() for w in words}

print(sorted(first_letters))

Output:

['a', 'b', 'c']

Dictionary Comprehensions

Use curly braces with a key: value pair:

{ key_expression: value_expression   for item in iterable }
squares = {n: n * n for n in range(1, 6)}
print(squares)

names = ["Asha", "Ravi", "Kiran"]
name_lengths = {name: len(name) for name in names}
print(name_lengths)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
{'Asha': 4, 'Ravi': 4, 'Kiran': 5}

Combined with items() from the Dictionaries chapter, you can filter or transform an existing dictionary:

prices = {"pen": 10, "book": 250, "bag": 800}

expensive = {item: p for item, p in prices.items() if p > 100}
discounted = {item: p * 0.9 for item, p in prices.items()}
swapped = {p: item for item, p in prices.items()}

print(expensive)
print(discounted)
print(swapped)

Output:

{'book': 250, 'bag': 800}
{'pen': 9.0, 'book': 225.0, 'bag': 720.0}
{10: 'pen', 250: 'book', 800: 'bag'}

When swapping keys and values, remember that keys must be unique. If two items had the same price, the later one would overwrite the earlier one.


Generator Expressions

If you use parentheses instead of square brackets, you don't get a "tuple comprehension". You get a generator expression, which produces values one at a time, only when they are needed, instead of building the whole list in memory.

total = sum(n * n for n in range(1, 6))
print(total)

has_negative = any(x < 0 for x in [4, -1, 7])
print(has_negative)

gen = (n * n for n in range(3))
print(type(gen))
print(list(gen))

Output:

55
True
<class 'generator'>
[0, 1, 4]

When a generator expression is the only argument of a function like sum(), max() or any(), you can drop the extra parentheses. Generator expressions are great for large data, because they use almost no memory. You'll learn how generators work in detail in the Iterators & Generators chapter. If you really want a tuple, write tuple(x for x in ...).


Scope: The Loop Variable Stays Inside

In a normal for loop, the loop variable still exists after the loop. In a comprehension, it doesn't leak out:

x = "original"

squares = [x * x for x in range(4)]

print(x)
print(squares)

Output:

original
[0, 1, 4, 9]

When NOT to Use a Comprehension

Comprehensions are meant to make code clearer. If one gets hard to read, a normal loop is better.

  • Too much logic. More than two for clauses or multiple conditions usually means a loop would be clearer.

  • Side effects only. Don't write [print(x) for x in items]. It builds a useless list of None values. Use a plain for loop to do something; use a comprehension to build something.

  • You need break. A comprehension always goes through the whole iterable.

Performance-wise, a list comprehension is usually a little faster than a loop with append(), because the looping happens in optimized internal code. Both are O(n) for n items, so choose based on readability first.


Common Mistakes

  • Putting else at the end. A filter if can't have an else; move the if-else to the front.

  • Expecting (x for x in ...) to be a tuple. It's a generator, and it can only be used once.

  • Getting nested for order backwards. Write them in the same order as you would in nested loops.

  • Using [[0] * n] * m for a grid. Use [[0] * n for _ in range(m)].

Quick Reference

Type

Syntax

Result

List comprehension

[x * 2 for x in data]

list

With filter

[x for x in data if x > 0]

list

With if-else

[x if x > 0 else 0 for x in data]

list

Set comprehension

{x % 3 for x in data}

set

Dict comprehension

{x: x * x for x in data}

dict

Generator expression

(x * 2 for x in data)

generator

Final Takeaway

A comprehension turns the "empty list + loop + append" pattern into a single readable line. Remember the shape: expression first, then the for, then an optional if filter.

Practice problems:

  • Build a list of all numbers from 1 to 50 that are divisible by both 3 and 5.

  • Given a sentence, build a dictionary mapping each word to its length.

  • Given a list of strings, build a set of all unique vowels used in them.

  • Flatten [[1, 2], [3], [4, 5, 6]] into a single list using one comprehension.