Python 3.14 ยท
โ verified by execution on 2026-07-23
List comprehensions provide a concise way to create lists. They are generally faster and more readable than standard loops.
Notice how the inner scope is evaluated:
python ยท visualize
squares = [x**2 for x in range(5)]print(squares)
Filtering Elements
A list comprehension can include an if clause to filter elements.
python
evens = [x for x in range(10) if x % 2 == 0]print(evens)
Output
[0, 2, 4, 6, 8]
Your output
Nested Loops
You can nest for clauses in a list comprehension, and the order is the same as in nested for loops.
python
matrix = [[1, 2], [3, 4]]flat = [num for row in matrix for num in row]print(flat)
Output
[1, 2, 3, 4]
Your output
Tuples in Comprehensions
If the expression in a list comprehension evaluates to a tuple, it must be parenthesized.
python
res = [(x, x**2) for x in range(3)]print(res)
Output
[(0, 0), (1, 1), (2, 4)]
Your output
Set and Dictionary Comprehensions
Python also supports set comprehensions using curly braces.
python
unique_chars = {c for c in 'abracadabra' if c not in 'abc'}print(sorted(list(unique_chars)))
Output
['d', 'r']
Your output
Dict comprehensions allow creating dictionaries from arbitrary key and value expressions:
python
squares_dict = {x: x**2 for x in (2, 4, 6)}print(squares_dict)
Output
{2: 4, 4: 16, 6: 36}
Your output
Edge Cases
Missing Parentheses on Tuples: Forgetting parentheses around a tuple expression raises a SyntaxError: [x, x**2 for x in range(3)].
Empty Iterables: Empty iterables produce empty collections without error: [x for x in []] returns [].
Check yourself
What does [n * n for n in range(4)] produce?
Reveal answer
[0, 1, 4, 9] โ range(4) gives 0, 1, 2, 3, and each value is squared: 0, 1, 4, 9.
Which comprehension keeps only the even numbers from nums?
Reveal answer
[n for n in nums if n % 2 == 0] โ A filtering if goes at the END of the comprehension: [n for n in nums if n % 2 == 0].
In Python 3, does the loop variable x still exist after [x for x in nums] runs?
Reveal answer
No โ a comprehension has its own scope, so x does not exist outside it. โ Unlike a plain for-loop, a comprehension has its own scope, so its loop variable never leaks out.
What does (x for x in nums) create?
Reveal answer
A generator expression โ wrap it in tuple(...) if you want a tuple. โ Parentheses make a lazy generator expression, not a tuple. Use tuple(x for x in nums) to get a tuple.
Challenges
Challenge 1 +15 XP
Use a list comprehension to create a list named `odds` containing only the odd numbers from the provided list `numbers`.
python
Test 1 โ expects "[5, 9, 21]"
Need a hint? (โ25% XP)
Review the syntax: [expression for item in iterable if condition]
Show solution (0 XP)
numbers = [12, 5, 8, 9, 21, 4]odds = [x for x in numbers if x % 2 != 0]print(odds)
Challenge 2 +15 XP
Use a dictionary comprehension to create a dictionary named `word_lengths` that maps each word in the list `words` to its length.
python
Test 1 โ expects "{'hello': 5, 'world': 5, 'python': 6}"
Need a hint? (โ25% XP)
Review the syntax: [expression for item in iterable if condition]
Show solution (0 XP)
words = ['hello', 'world', 'python']word_lengths = {w: len(w) for w in words}print(word_lengths)