MemotivaGenerators and yield

Why use a generator instead of a list?

Generators and yield

Audio flashcard · 0:16

Nortren·

Why use a generator instead of a list?

0:16

Generators produce values lazily, computing each one on demand. They use constant memory regardless of the total number of values, while a list of equivalent contents uses memory proportional to the number of elements. Generators are ideal for streaming data, infinite sequences, and pipelines.
docs.python.org