Lesson 7 of 21 · 50 min · UG / professional
Comprehensions — the short spelling of a filter loop
Keep heat days; double a list of quantities. The long form is a new list plus append. The one-liner on a laptop is a comprehension. Debug the long form at 1 a.m.
Weather station
Filter a table of readings.
You will be able to
- ✓ Build a filtered list with a loop and append (the long form)
- ✓ Build a mapped list — new values, same count — the same way
- ✓ Recognise a comprehension as the laptop one-liner for that loop
Keep heat days; double the quantities
The DHT logger wrote 28, 31, 34. Keep temperatures at or above 32. That is a filter: a new list, an if, append. Canteen packs of 2, 4, 1 become 4, 8, 2 for a double-shift indent. That is a map: every input produces one output, append each. Neither loop should overwrite the source — the logger file is evidence.
On a laptop, [t for t in temps if t >= 32] and [q * 2 for q in qty] are the short spellings. They are not a new language. Debug the long form at 1 a.m. when the one-liner hid a wrong cutoff. This sandbox forbids the short spelling so you cannot hide.
Filter and map are different jobs
A filter’s result is often shorter. A map’s result is the same length. If you mix them — keep heat days and also convert to Fahrenheit in one confused loop — name two lists or two functions. Comprehensions can nest on a laptop; nested confusion is still confusion.
When the “map” is a GST function you already wrote, call it inside the loop. The comprehension on a laptop would call it too. The function is the reusable part; the loop is only the walk.
Laptop: the same two jobs as one-liners
temps = [28, 31, 34, 29] hot = [t for t in temps if t >= 32] qty = [2, 4, 1] doubled = [q * 2 for q in qty] print(hot) print(doubled)
Words that matter
- Filter
- A new list of items that pass a test — often shorter.
- Map
- A new list of transformed items — usually the same length.
- Comprehension
- Laptop short spelling of a filter or map loop.
Common mistakes
Avoid: Erasing the only raw list while filtering heat days.
Do this: append keepers (or mapped values) to a new list.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — filter heat days
Keep >= 32. Source length stays 4. 34 is a keeper.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — map quantities
Each pack doubled. Length stays 3. 8 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Map through a named GST function
The loop only walks. The rule lives in add_gst.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Keep passes
marks = [32, 41, 55]. Collect values >= 40. Print so 41 and 55 appear.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.