Legends ofPythos
Claim your name
Working with Data

List comprehensions

Lesson 7 of 14

Watch the lesson1:13 · with Torsten
Mental model: Think of a list comprehension as an assembly line with two stations: a filter and a transformer. The filter checks each item; if it passes, the transformer reshapes it into something new. You never see items that fail the check.

The basic form

# Old way
squares = []
for n in range(5):
    squares.append(n * n)
print(squares)  # [0, 1, 4, 9, 16]

# Comprehension way
squares = [n * n for n in range(5)]
print(squares)
Both produce the same list.
The pattern is [expression for item in iterable]. Read it right-to-left: take each item from the iterable, compute the expression, and collect the results into a new list. No .append() needed.

Filtering with if

# Keep only even numbers, then square them
even_squares = [n * n for n in range(10) if n % 2 == 0]
print(even_squares)
[0, 4, 16, 36, 64]
The if clause acts as a filter. Items that fail the test are skipped entirely — they never reach the expression part.

When to use one over the other

nums = [1, 2, 3]
data = [{'name': 'ada'}, 'not a dict', {'id': 7}]

# Clear as a comprehension
doubles = [x * 2 for x in nums]

# Harder to read — a plain loop is fine here
result = []
for item in data:
    if isinstance(item, dict) and 'name' in item:
        result.append(item['name'].upper())
Readability beats cleverness.

Your turn

0 of 3 solved

Exercise 1

+30 XP
A five-a-side tournament log stores the goal counts for each team in goals. Write a list comprehension that squares every entry to produce an energy rating.
goals = [1, 0, 5, 2]
result = None

Run your code to check it against the tests.

Exercise 2

+30 XP
Using a list comprehension with an if filter, build a new list from words that contains only the words longer than 4 characters.
words = ['hi', 'hello', 'hey', 'world', 'go']
result = None

Run your code to check it against the tests.

Exercise 3

+30 XP
Use a single list comprehension. The variable cubes_even must contain the cube of every even number in numbers.
numbers = [1, 2, 3, 4, 5, 6]
cubes_even = None

Run your code to check it against the tests.