Python Medium · Lesson 1 · On the house

Comprehensions

Build a list in one honest line. Lesson 1 is free — on the house.

Lessons · Python Medium · Lesson 1 of 10 · On the house

Comprehensions

Build a list in one honest line.

Illustration for the Python course
Build a list in one honest line.

Introductory and Easy are assumed: names, branches, loops, functions, slices, f-strings, imports, and basic exceptions. Medium adds sharper tools.

A list comprehension builds a list from an iterable in one expression: [n * n for n in range(4)] yields [0, 1, 4, 9]. Read it as n*n for each n in range(4). It is a loop-and-append compressed into an expression, not magic syntax from another language. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Name the idea once, reuse it cleanly. If you find yourself copy-pasting the same three lines, that is a function (or a generator) knocking politely. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

Filter with if after the iterable: [n for n in range(10) if n % 2 == 0]. Multiple for clauses nest left to right like nested loops — keep them shallow so humans can still breathe while reading. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Disk is slower and more permanent than memory. Be deliberate about modes, paths, and closing. Prefer with-blocks so cleanup isn't a hope. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

Comprehensions make new lists; they do not mutate the source. If you need side effects like printing each item, use a plain for-loop. Comprehensions are for building collections with a clear transform. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

Dict and set comprehensions exist too. Generator expressions use parentheses and stay lazy — that patience belongs more in Expert. Today we master list comprehensions and the habit of expanding them back into a for-loop on paper. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Think labels on shelves, not seats in a row. You ask by name. If the label is missing, [] screams; .get can shrug with a default — know which behavior you want. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

When a comprehension grows past one transform and one filter, a written for-loop is often kinder to tomorrow-you. Clarity beats golf. Medium is about sharper tools, not denser puzzles. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

The same half-open range rules apply. Trace range(4) as 0,1,2,3 then square each value. If you can expand the comprehension to a loop, you understand it; if you cannot, you are memorizing shapes. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Disk is slower and more permanent than memory. Be deliberate about modes, paths, and closing. Prefer with-blocks so cleanup isn't a hope. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

Put Comprehensions in a sentence you could teach a friend: what changes in the program, what stays the same, and what output you expect from the still. Say that back in your own words before you scroll — if you can't, the still for Comprehensions will feel like typing karaoke. Before you run anything, predict what Comprehensions should do on the still's inputs. Prediction turns running into confirmation.

If you are skimming, stop and re-read the still's lines in order while pointing at each token. Comprehensions is not a vocabulary flashcard — it is a behavior you should be able to simulate on paper.

When an exercise asks you to match the still, match it honestly: same names, same structure, same output idea. Clever shortcuts that skip the concept will leave a hole that the next lesson falls into.

Open-book does not mean guess. The right answer is on this page in plain language. Wrong choices are usually near-misses that sound technical; pick the claim that matches what we actually traced.

comp

squares = [n * n for n in range(4)]
print(squares)
Map each n to n*n into a new list.

range(4) yields 0 through 3. Each n*n is collected into squares. print shows [0, 1, 4, 9]. That is the same work as starting with [] and appending inside a for-loop.

Pitfalls: using comprehensions for side effects; nesting until unreadable; forgetting the expression before for; mixing up list comps with generator expressions. Expand to a for-loop whenever you feel unsure.

Quiz

[n * n for n in range(4)] equals?

Quiz

Where does a filter if go in a comprehension?

Quiz

Do comprehensions mutate the source iterable?

Quiz

When is a plain for-loop better?

Quiz

range(4) yields which values?

Check

Match the comprehension still.

One honest line that builds a list. Next: dict.get — miss without a crash.

Open-book: the answers are on this page. Pass every quiz and check (6) to mark the lesson done. This visit: 0/6.

Comprehensions · Python Medium (free) — The Gold Standard