Python Fundamentals / Collections
Lesson 28 of 38
List comprehensions
A list comprehension builds a new list in one line: [expression for item in items if condition].
Key points
- Shorter than a for loop with append
- Optional if filter at the end
- Keep them readable
evens = [n for n in range(10) if n % 2 == 0]
print(evens) # [0, 2, 4, 6, 8]Quiz · +10 XP
What does [x * 2 for x in [1, 2, 3]] give?
Log in to save progress and earn XP.