Predict List Comprehension with Filter
Predict Output Medium
Question
What does this code print?
Code
nums = [1, 2, 3, 4, 5, 6]
evens = [x for x in nums if x % 2 == 0]
print(evens)
Hint
The if clause filters which items are included.
Explanation
The comprehension iterates over nums and includes only items where x % 2 == 0 (even numbers). From [1, 2, 3, 4, 5, 6], the even numbers are [2, 4, 6]. Output: [2, 4, 6]
Related Resources
Related Lessons
- List Comprehensions
Filtering with if.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.