Remember
A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.
A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)Top-level scope
range(0, 5)Object 7 · list[0, 4, 16]A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)
|
v
DISPLAYED RESULT
[0, 4, 16]
FINAL BINDINGS
numbers --> object 1 (range)
squares --> object 7 (list)
object 1: range(0, 5)
object 7: [0, 4, 16]Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)Top-level scope
range(0, 5)Object 7 · list[0, 4, 16]The example has finished. Compare the final output with the values and references above.
[0, 4, 16]
What do you expect this program to print?
numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)In Python, there's a short and neat way to create new lists. It’s called list comprehension. It looks a little different from what you've seen before, but don't worry — you'll understand it step by step.
List comprehension is a way to create a new list by doing something to each item in another list (or a range of numbers).
Instead of writing a loop and appending to a list, you can write it all in one line.
Let’s first see how we normally make a new list from numbers 1 to 5:
numbers = []
for i in range(1, 6):
numbers.append(i)
print(numbers)
[1, 2, 3, 4, 5]
numbers = [i for i in range(1, 6)]
print(numbers)
[1, 2, 3, 4, 5]
i for i in range(1, 6) means: “for every number i from 1 to 5, take i and put it into the list.”[ ] mean we’re making a list.squares = [i*i for i in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]
Why? Because:
i = 1 → 1*1 = 1i = 2 → 2*2 = 4doubles = [i*2 for i in range(1, 6) if i % 2 == 0]
print(doubles)
[4, 8]
Why?
i % 2 == 0 checks if a number is evenwords = ["apple", "banana", "cherry"]
uppercased = [word.upper() for word in words]
print(uppercased)
['APPLE', 'BANANA', 'CHERRY']
Why?
word.upper() turns each word into uppercase.words = ["apple", "banana", "cherry", "blueberry"]
starts_with_b = [word for word in words if word.startswith("b")]
print(starts_with_b)
['banana', 'blueberry']
Why?
word.startswith("b") checks if the word starts with the letter “b”.List comprehension is just a shortcut to make lists. It’s clean and easy once you practice a few times.
Here's the basic pattern to remember:
[expression for item in collection if condition]
You can skip the if part if you don’t need to filter anything.
Connect what you noticed in the example with the idea below.
A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.
A list comprehension always modifies the source collection.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.