Generators

A generator produces its values on demand, one at a time, instead of building the whole list in memory. You create it with a function containing yield or a generator expression. It is useful for large sequences or streams, but can only be iterated once.

Example

def compte(n):
    for i in range(n):
        yield i

Related concepts

The whole glossary