Compressor — Comprehensions
Six loops to squeeze into one line each. The output must match exactly, and append() is banned.
How to play
Each level hands you a working loop, or the data it would run over, and asks for the comprehension that does the
same job. The result is compared exactly and append() is blocked, so you cannot sidestep the lesson by
keeping the loop.
What each level teaches
- Squares — the basic shape: expression, then
for. - Keep only some — a trailing
ifthat filters. - Change every item — the expression can call methods and functions.
- One of two values — a conditional expression, which goes before the
for. - Build a dictionary — braces and a
key: valuepair. - Sets and two fors — braces without a colon, and nesting.
The two ifs, and why they get mixed up
A comprehension can contain an if in two completely different places, doing two different jobs:
- After the for — it filters.
[n for n in nums if n > 3]decides whether an item is collected at all. Noelseis allowed, because there is nothing to put in its place. - Before the for — it chooses.
["big" if n > 3 else "small" for n in nums]collects every item but picks which value. Here anelseis required.
Levels 2 and 4 exist to separate those permanently. If you ever get a SyntaxError on a comprehension
containing an if, it is almost always this — a filtering if that was handed an
else, or a choosing if written after the for.
When not to use one
Comprehensions are not automatically better. They win when the transformation fits comfortably on one line and
reads like a sentence. They lose when there are several conditions, nesting beyond two for clauses, or
any side effect — a comprehension written purely to call print() is a loop in disguise and should be
written as one. The honest test: if a reader has to go through it twice, write the loop. The
lists tutorial covers the syntax in prose.
Frequently asked questions
What is a list comprehension in Python?
A one-line way to build a list from an iterable: [expression for item in iterable]. It replaces the pattern of creating an empty list and appending to it inside a loop.
Where does the if go in a list comprehension?
After the for when it filters — [n for n in nums if n > 3] — and before the for when it chooses between two values, in which case an else is required: ["big" if n > 3 else "small" for n in nums].
How do I write a dictionary comprehension?
Use braces and a colon: {w: len(w) for w in words}. Braces without a colon give a set comprehension instead.
Are comprehensions faster than loops?
Usually slightly, because the append is handled internally rather than as a method call each time. The real reason to use one is readability, and the speed difference rarely matters.
When should I not use a comprehension?
When it needs more than about one line to understand: several conditions, deep nesting, or any side effect. A comprehension written for its side effects should be a plain for loop.
Keep going
- Read the comprehensions tutorial — the written explanation behind every level here.
- Variable Vault — variables & data types.
- Operator Arcade — numbers & operators.
- String Surgeon — strings & slicing.
- 20 Python practice exercises with worked solutions.
Take the games with you
Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.
Get it on Google Play