Python Comprehensions Playground

[expr for x in it if cond] = a for-loop folded into one line. Build the slots, see the result + the equivalent for-loop. Brackets decide list/set/dict. An…

Why it matters

Comprehensions are everywhere in Python. Reading one means knowing the order it runs in (for, if, then the expression), the order of nested fors, and that its brackets decide its kind: a list, a set, a dict, or a generator used up once.

The key idea

A comprehension is a folded for loop: for, then if, then the expression

Common mistakes

Try it in this playground