Step: List comprehension: filter
List comprehension: filter
Adding if condition to a comprehension filters which items make it into the result — only items where the condition is true are kept.
Why it matters: filter + transform in one line covers most everyday list processing.
Example: [n for n in [1, 2, 3, 4] if n > 2] is [3, 4].
Your turn: keep only the even numbers using n % 2 == 0.
Pitfall: the if here is a filter clause, different from the x if cond else y conditional expression — mixing them up is a common source of confusion.
Setup:
Your code:Expected output:
Step 6 of 18