1.2.34 · D3Introduction to Programming (Python)

Worked examples — List comprehensions — `[expr for x in iterable if condition]`

3,969 words18 min readBack to topic

This is the "throw everything at it" companion to [[1.2.34 List comprehensions — `[expr for x in iterable if condition]`|the parent note]]. There, we learned the shape [expr for x in iterable if condition]. Here, we run it through every kind of situation a comprehension can meet, so that no future problem is a surprise.

We assume nothing beyond the parent. If a word like "iterable" or "ternary" appears, we re-anchor it the moment we use it.


The scenario matrix

Before working examples, let us map the whole territory. A list comprehension has three slots — expr, for, if — and each slot can be in one of a few "states". The table below lists every distinct case class this topic can throw at you. Every later example is tagged with the cell it covers.

# Case class What is special about it Covered by
A Transform only (no filter) Every item survives; only expr acts Example 1
B Filter only (expr = item) Nothing changes; items just get kept/dropped Example 2
C Transform + filter together Both slots active Example 3
D Ternary in expr (a if c else b) Choosing a value, if/else goes BEFORE for Example 4
E Ternary AND filter in one line Both ifs present, in different places Example 5
F Nested for (flatten / grid) Two loops, outer-to-inner order Example 6
G Degenerate input: empty iterable, or filter kills all Result is [] — the zero case Example 7
H Real-world word problem Translate English → comprehension Example 8
I Exam twist: nested for + filter that depends on both loop variables Order of evaluation must be reasoned exactly Example 9
J Chained filters (multiple ifs after one for) Two ifs in a row act as AND Example 10

We will now hit every cell A through J.


Example 1 — Case A: transform only


Example 2 — Case B: filter only


Example 3 — Case C: transform AND filter


Example 4 — Case D: ternary inside expr


Example 5 — Case E: ternary AND filter in one line


Example 6 — Case F: nested for (flatten a grid)


Example 7 — Case G: degenerate inputs (the zero case)


Example 8 — Case H: real-world word problem


Example 9 — Case I: exam twist (nested for + filter on both variables)


Example 10 — Case J: chained filters (multiple ifs after one for)


Recall Which cell was which?

Match example → matrix cell. Example 4 (n if n>0 else 0) hits which case? ::: Case D — ternary in expr, if/else before for. Example 7 hits which case? ::: Case G — degenerate inputs, result is []. Example 9's filter uses both i and j — is that legal? ::: Yes; a filter may use any variable introduced to its left (Case I). In Example 8, does the filter see the pre-tax or post-tax price? ::: Pre-tax — the filter runs before expr. Are two ifs in a row after one for allowed? ::: Yes — chained filters (Case J), behaving like and.


Connections

  • For loops — every example was first written as an honest loop, then collapsed.
  • Conditional expressions (ternary) — the a if c else b in Examples 4 and 5.
  • map() and filter() — Examples 1–2 are map/filter in disguise.
  • Dictionary and set comprehensions — same cells, different brackets.
  • Generator expressions — swap [] for () to get these results lazily.
  • range() and iterables — feeds the for slot in nearly every example.

Case Map

The diagram below is a pure recap: the central node is the scenario matrix, and each branch is one case class (A–J) with the example that covers it. Use it as a checklist — if you can name the trick each branch tests, you have covered the whole territory.

order rule reused

value choice

dropping

dropping

Scenario matrix A to J

A transform only - Ex1

B filter only - Ex2

C transform plus filter - Ex3

D ternary in expr - Ex4

E ternary and filter - Ex5

F nested for - Ex6

G empty result - Ex7

H word problem - Ex8

I exam twist - Ex9

J chained filters - Ex10