The Selective Assembly
In the last lesson, Hoppy compressed “iterate and transform” into a shorter assembly line. But real work at the craft bench quickly adds one more small step: some materials should be checked first, and only the ones that pass should be polished and kept.
This is where a list comprehension can do a light filter while it builds a new list. Today we add only one simple if condition, so you can clearly see two things: which items are allowed into the new list, and what they become after they get in.
Filter first, then place the chosen materials into the new list
A conditional list comprehension is still the same basic move from the previous lesson, just with one light gate added. The part before for decides what the kept material will look like, and the if at the end decides which materials can pass through that gate.
raw_labels = [" moss lantern ", "ash pin", " mist bead", "fern note"]
selected_labels = [
raw_label.strip().title()
for raw_label in raw_labels
if raw_label.strip().startswith("m")
]
print("Selected labels:", selected_labels)
The filter here stays light: only labels whose cleaned text starts with "m" are kept. You can read it as “check whether it qualifies, then assemble its final form.” Notice how controlled this stays. The condition is simple, and the transformation is simple, so the whole line is still readable.
Filtering and transforming can travel together, but do not crush readability
The most important judgment in this compare lesson is this: a light filter can live next to a light transformation inside a comprehension, but not every situation should be forced into that shape. The moment the condition gets twisty or the transformation gets messy, a normal loop becomes the better choice again. Today we are only practicing the “just enough” version, not stuffing all logic into one line.
Stop building selected_labels with an empty list, a loop, and append. Replace that setup with one list comprehension that includes an if filter.
The result part still uses raw_label.strip().title(), which tells Python how each kept label should look. The condition uses if raw_label.strip().startswith("m"), which tells Python which labels are allowed in.
When you run the code, the result should only contain the labels that passed the gate. If the condition and transformation start fighting for attention, that is your sign to go back to a normal loop.
A small, easy-to-read filter such as a prefix, suffix, length, or membership check. If the condition keeps growing or needs more branches, stop and switch back to a normal loop.
Suggested SolutionExpandCollapse
raw_labels = [" moss lantern ", "silver fern ", " mist bead", "ash pin"]
selected_labels = [
raw_label.strip().title()
for raw_label in raw_labels
if raw_label.strip().startswith("m")
]
print("Selected labels:", selected_labels)Advanced TipsWant more? Click to expandClick to collapse
This lesson intentionally avoids complex conditions, and it does not introduce ternary expressions. The only goal for now is to let “filtering” and “building a new list” sit together lightly while the code still stays easy to read.
If you later meet cases with heavier conditions, do not feel forced to turn them into comprehensions. Knowing when to stop and choose a normal loop is part of good readability judgment.