Recipes: Iterators and generators¶
Task-focused how-tos. If you know what you want to do but aren't sure how to build it, this is the section to skim.
Recipes in this section¶
- Process a large file lazily — streaming a file line by line (or record by record) without loading it all into memory. The pattern that lets you process multi-gigabyte inputs on a laptop.
- Chain and group iterables —
itertools.chainfor concatenation,groupbyfor runs of adjacent equal values,zipandzip_longestfor parallel iteration. - Combine generators into a pipeline — stacking
filter → transform → window → aggregateas generators that feed each other. The functional-pipeline style for data processing. - Avoid common iterator mistakes — a catalogue of the gotchas: once-consumed iterators, mutating during iteration, generator-inside-a-try, late binding in generator expressions.