Generators

yield, laziness, and generator expressions vs list comprehensions

30 min · interactive lesson with runnable code and 6 graded exercises.

What you'll be able to do

  • Explain how yield pauses and resumes a function
  • Say why generators are memory-efficient
  • Choose between a generator expression and a list comprehension
  • Write batching and streaming generators

The full lesson is free with an account.

yield pauses the function

Why they save memory

Generator expressions

List or generator?

Batching and streaming

Infinite streams

Common pitfalls