Comprehensions build brand-new collections in a single expression: transform every element, filter some out, done.
1 Loop Power, One Line
A comprehension builds a whole list in a single expression — transform, filter, finished.
2 Fixed Word Order
Every comprehension reads the same way: expression, then for, then an optional if.
3 Dicts Too
Swap the brackets for braces and you build dictionaries instead of lists.
4 Fresh Results
Comprehensions never mutate the source — you always get a brand-new collection.
A whole loop in one line
Loop Power, One Line1A comprehension builds a whole list in a single expression — transform, filter, finished.
Fixed Word Order2Every comprehension reads the same way: expression, then for, then an optional if.
Dicts Too3Swap the brackets for braces and you build dictionaries instead of lists. Fresh Results4Comprehensions never mutate the source — you always get a brand-new collection. 4 building blocks make up A whole loop in one line
One comprehension doubles every number; a second keeps only the odds. Watch both lists assemble.
mainnums is the source list — the comprehensions will visit every element once.
Console
output appears as the program runs…
A dict comprehension and a filtered list — trace them element by element.
Your predicted output — one line per console line
Hint: A dict comprehension writes key: value pairs — here each word maps to its length. Only "ben" scores 5 or more.
A comprehension builds its result slot by slot, in order — the original nums never changes.
Drag to orbit the scene. Press Play to watch memory in action.
Click a term, then click its description to lock the pair.
5 conceptsTap a card to expand ?[n * 2 for n in nums]drops into place
?[n for n in nums if n > 2]drops into place
?{w: len(w) for w in words}drops into place
?for n in numsdrops into place
?if n % 2 == 1drops into place
01[expr for x in seq] builds a new list; [expr for x in seq if cond] filters first.
02{k: v for x in seq} builds a dict the same way — key and value from one loop.
03The word order is fixed: expression, then for, then the optional if.
04Comprehensions replace the loop-and-append dance with one readable line.
05The source collection is never modified — you always get a new object back.