← Python Intermediate
1 / 25
Lesson 1 — Comprehensions with a filter
Concise loops
A comprehension builds a list in one line: [expr for x in items if cond]. The expression decides what goes in, the for part says where items come from, and the optional if part keeps only the items that pass. Python reads it left to right, just like the loop it replaces.
names = ["ann", "bob", "al"]
short = [n for n in names if len(n) < 3]
print(short) # ['al']
Why it matters: filtering and transforming lists is daily work, and comprehensions make it short and readable.
Want it another way?
Task
Build a second list with the squares of only the even numbers 0..9 and print it.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.