Python List Comprehension: Complete Guide with Examples

List comprehension is Python's most elegant feature — and one of the most misunderstood. Once you master it, you will write cleaner, faster, more Pythonic code. This guide covers everything from basic syntax to advanced patterns, with real examples.

1. Basic Syntax

# Traditional loop
squares = []
for x in range(10):
    squares.append(x ** 2)

# List comprehension — same result, one line
squares = [x ** 2 for x in range(10)]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

The pattern is: [expression for item in iterable]

2. Adding Conditions (Filter)

# Only even squares
even_squares = [x ** 2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]

# Filter names
names = ["Alice", "Bob", "", "Charlie", ""]
valid = [name for name in names if name]
# ['Alice', 'Bob', 'Charlie']

3. If-Else in the Expression

# If-else in the expression (NOT the filter)
labels = ["even" if x % 2 == 0 else "odd" for x in range(6)]
# ['even', 'odd', 'even', 'odd', 'even', 'odd']

# Difference: filter is at the END, expression with if-else is at the START
filtered_labels = [x for x in range(6) if x % 2 == 0]
# [0, 2, 4]

4. Nested Comprehensions

⚠ Watch out: Nested comprehensions read left-to-right in the order of nested for loops (outer to inner), which is the opposite of mathematical notation. If a nested comprehension spans more than two lines, rewrite it as a regular loop — your future self and your teammates will thank you.
# Flatten a 2D list
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Pairs
pairs = [(x, y) for x in range(3) for y in range(2)]
# [(0,0), (0,1), (1,0), (1,1), (2,0), (2,1)]

5. Dictionary and Set Comprehensions

# Dictionary comprehension
squares_dict = {x: x ** 2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

# Set comprehension — automatically deduplicates
numbers = [1, 2, 2, 3, 3, 3]
unique = {x for x in numbers}
# {1, 2, 3}

6. Common Mistakes

Mistake 1: Comprehension too complex

# Bad — unreadable
result = [x for x in data if x > 0 if x % 2 == 0 if x < 100 for y in related if y]

# Good — use a regular loop
result = []
for x in data:
    if x > 0 and x % 2 == 0 and x < 100:
        for y in related:
            if y:
                result.append(x)

Mistake 2: Modifying the original list in-place

# Comprehension creates a NEW list — it does not modify the original
data = [1, 2, 3]
[x * 2 for x in data]  # Returns [2, 4, 6]
print(data)  # Still [1, 2, 3]

7. When to Use (And When Not To)

Use Comprehension WhenUse Regular Loop When
The operation is simple (map/filter)The logic has multiple conditions
The result fits in one expressionYou need side effects (logging, writing to files)
A reader can understand it in under 10 secondsThe comprehension is longer than 2 lines
A list comprehension should be obvious at a glance. If you need to squint, use a loop.

FAQ

Are list comprehensions faster than loops?

Yes. They are implemented in C and are usually 10-30% faster than equivalent for-loops. But readability > micro-optimization.

Can I use break or continue in a comprehension?

No. Comprehensions do not support break or continue. Use a regular loop if you need these.

What is a generator expression?

Same syntax but with parentheses: (x*2 for x in range(10)). Yields items lazily one at a time — memory efficient for huge datasets.