LevelJar Sign in

Python course / Advanced

Iterators and generators

Write generators that hand out values one at a time, and chain them into pipelines over data too big to hold. A generator you have already used up gives a silently wrong answer rather than an error.

  • iter and next
  • yield
  • laziness
  • endless generators
  • pipelines
  • yield from
  • tee
  • cleanup

Start this module

Runs in your browser. Start straight away; create an account when you want your progress kept.

values handed out one at a time
def readings(rows):
    for row in rows:
        yield int(row.strip())


def above(values, limit):
    for value in values:
        if value > limit:
            yield value


rows = [" 4\n", "9\n", " 12 \n", "7\n"]
pipeline = above(readings(rows), 5)

print(type(pipeline).__name__)
print(list(pipeline))
print("used up:", list(pipeline))
generator
[9, 12, 7]
used up: []

What you will learn

The lessons in this module

  • What a for loop really does
  • Iterables and iterators
  • Writing an iterator class
  • Generator functions: yield
  • What calling a generator does, and does not do
  • Endless generators
  • Generator expressions, from the inside
  • The generator that was already used up
  • When you need a generator's values twice
  • Pipelines
  • yield from, and what a generator returns
  • Cleaning up after a generator
  • send(): values going the other way
  • Generators that remember
  • Iterable classes, the usual way
  • iter() with a sentinel
  • When not to use a generator
  • Measuring what laziness saves
  • Capstone: streaming the stall's log
  • Module review: the fridge sensor

Ready when you are

Start this module

Previous: Standard library tour. Next: Decorators. All modules are on the course page.