2.11 Comprehensions, iterators and generators
You can process a file larger than your RAM.
Before:00. Orientation & SetupUnlocks:03. Data Handling & Analysis04. Classical AI — Agents, Search & Knowledge Representation
Generators process data lazily, one item at a time, which is how a file larger than RAM gets handled in constant memory; comprehensions are the idiomatic fast way to build collections when they do fit. It opens the advanced Python run because lazy pipelines underlie the data tooling ahead. The catch worth engraving: generators are single-use — iterate one a second time and the second pass is silently empty, which produces some of Python's most confusing bugs.
Work through these
List, dict, set comprehensions
Building a list, dictionary or set from an expression over an iterable, which is Python's most characteristic construct. Once it is fluent, a great deal of loop code disappears.
The iterator protocol
The protocol that makes something loopable, which is simpler than it appears and is what unifies every collection in the language. Knowing it explains why so many things work with a loop.
Generators, yield, and lazy pipelines
Generators produce values one at a time instead of building a list, which is what lets you process a file larger than memory. Chaining them creates pipelines that stay lazy.
itertools worth memorizing
A handful of tools from the iteration library that solve common problems compactly. Knowing which ones exist is more valuable than memorising their signatures.
Sign in to keep your progress.
Free resources
We haven't checked most of these for screen reader use yet.
Links last checked 29 Aug 2026.
Stuck here?
Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.
Checking your session…
Topics shown in module order.