Coding Python
Home›Python Coding Games›Compressor

Compressor — Comprehensions

Six loops to squeeze into one line each. The output must match exactly, and append() is banned.

6 levels · Intermediate
Level 1

Checks
    Your PythonPython 3
    1

    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

    1. Squares — the basic shape: expression, then for.
    2. Keep only some — a trailing if that filters.
    3. Change every item — the expression can call methods and functions.
    4. One of two values — a conditional expression, which goes before the for.
    5. Build a dictionary — braces and a key: value pair.
    6. 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. No else is 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 an else is 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.

    Practise comprehensions on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    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

    ← All Python coding games  Read the tutorial →

    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
    Coding Python app icon