Unit 4 of 9 · about 4 hours · explain, implement

Iterators, generators and lazy pipelines

Data work streams. Single-pass exhaustion and silent re-iteration bugs are the usual pipeline failures.

You'll be able to

  • Implement a reusable iterator with tests for exhaustion, repeated iteration and invalid input.
  • Implement a lazy generator pipeline and compare it with the eager version on peak memory, time and correctness.
  • Predict generator cleanup on close, throw and deallocation, and PEP 479 StopIteration handling.

Covers

Iterable and iterator protocols, exhaustion and repeated iterationGenerators, lazy pipelines, send and cleanup behavior
1

Read

In this order, only the parts listed

0 of 5 done

  1. Comprehension forms and itertoolsStas' Python Cookbook - selected path

    Comprehensions, Iterators and itertools: list/dict/set/generator forms; the essential itertools (chain, islice, groupby on sorted input); zip strict=True and zip_longest

  2. Generators and yieldStas' Python Cookbook - selected path

    Generators and yield: the read_big line pipeline, infinite integers, yield from in flatten()

  3. Generator functions and container protocolsPython language reference

    Section 3.2.8.3 Generator functions (calling returns an iterator; the body runs on first next) and 3.3.7 Emulating container types (__len__, __iter__, __getitem__, __contains__)

  4. Custom IteratorsAlgoMaster Python - structured revision

    The Custom Iterators lesson and its exercise; write __iter__ and __next__ from memory first

  5. GeneratorsAlgoMaster Python - structured revision

    The Generators lesson and Exercise: Generators; then the yield Statement lesson for send and return values

2

Focus on

  • An iterable makes fresh iterators; an iterator is consumed once. iter() on a list gives a new object each time, while a generator is its own single-pass iterator.
  • Generators suspend at yield and resume after it. send() delivers a value to the paused yield expression; a return value becomes StopIteration.value; close() raises GeneratorExit inside.
  • Raising StopIteration inside a generator body is a RuntimeError since Python 3.7 by PEP 479; use return to stop.
  • Lazy wins memory only when nothing materializes. One list() call in the middle removes the gain; measure peak with tracemalloc on the same workload.
Common mistakes · 3
  • Re-iterating an exhausted generator and expecting fresh data. The second pass is empty with no error.
  • An accidental StopIteration inside a generator body. It surfaces as RuntimeError, not a quiet stop.
  • itertools.groupby across unsorted input. It groups consecutive items only; sort first or accept split groups.
3

Try it yourself

Closed-book, before checking anything

Closed-book, implement a repeatable Range iterable and a single-pass variant; assert the second list() pass is full for the first and empty for the second. Then run the eager versus lazy comparison: list of 1M squares peaks near 40 MB while the genexpr sum peaks under 1 KiB with equal results, measured with tracemalloc on CPython 3.14.4 default build, 12th Gen Intel Core i9-12900K.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.How do you test whether an object is an iterable or an iterator?Answer

    Call iter() twice: a list gives two distinct working iterators, while a generator returns itself. list() on an exhausted generator is [].

  2. 2.A generator yields 1 then raises StopIteration('x'). What does the caller see?Answer

    RuntimeError by PEP 479, not StopIteration. End a generator with return, optionally with a value.

  3. 3.How does a caller read a value returned by a generator, and how does it send one in?Answer

    return 'done' surfaces as StopIteration.value on exhaustion. send(v) resumes the paused yield expression with v; prime with next() first.

  4. 4.The lazy pipeline uses the same peak memory as the eager one. What is the first suspect?Answer

    Something materializes: a list(), sorted(), len() or reuse of the data downstream. Find the allocating line with tracemalloc.

5

Practice

  • AlgoMaster Python: Exercise: Custom Iterators

    Exercises 1 to 5, especially Exercise 2: Reusable Product Catalog Iterator and Exercise 5: Independent Iterators That Skip Closed Tickets

    Implement __iter__ and __next__ in your own file before checking.

  • AlgoMaster Python: Exercise: Generators

    Exercises 1 to 5, especially Exercise 3: Consume a Generator Once and Exercise 5: Flatten Nested Lists Lazily

    Add exhaustion and second-pass assertions the exercise does not ask for.

You're done when

Your iterator passes exhaustion, repeat and invalid-input tests, and your pipeline matches the memory numbers with equal outputs.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 5 more in the section
  • Stas' Python Cookbook - selected pathStas Bekman · primary

    Run Part I on dictionaries, iteration, functions, closures, decorators, classes and context managers; Part II on modules, files, paths, environment and arguments; and Part IV on versions, dependencies, packaging and code quality. Finish by building one src-layout package from a blank directory.

  • Python language referencePython Software Foundation · required

    Check Data model, Execution model, Import system and Exceptions whenever a claim depends on Python semantics. These pages are the authority when the course and a runtime observation disagree.

  • AlgoMaster Python - structured revisionAlgoMaster · support

    Use Functions, Modules and Packages, OOP, Advanced OOP, Exception Handling, Iterators and Generators, Type Hints and Annotations only after the cookbook pass. Open it for recall practice or a diagnosed weak mechanism, not as a second complete course.

Also in this section

  • Packaging Python ProjectsPython Packaging Authority · required

    Follow the packaging tutorial once from a blank directory, using a src layout, pyproject.toml, an isolated build and a clean-environment installation test.

  • dataclasses - data classesPython Software Foundation · reference

    Read the @dataclass parameters (eq, order, frozen, slots), field() with default_factory, frozen instances, post-init processing, replace, asdict and astuple. Use for the validated frozen dataclass in attributes-protocols-dataclasses.

  • typing - type hints and protocolsPython Software Foundation · reference

    Read Protocols for structural typing and runtime_checkable limits, Generics for TypeVar generics, and assert_type for narrowing checks. Use for the Protocol and narrowing parts of type-hints-protocols; PEP 695 syntax itself is in the language reference.

  • gc - cycle garbage collectorPython Software Foundation · reference

    Read the cycle collector interface: collect, get_count, garbage and disable/enable. Use for the cycle-collection and measurement parts of refcounting-gc-weakrefs.

  • weakref - weak referencesPython Software Foundation · reference

    Read weak-reference objects (ref callbacks, proxy objects) and the WeakValueDictionary example. Use for the weak-reference part of refcounting-gc-weakrefs.