Generators

Generators are lazy-evaluating data structures. The values are generated on demand which allows processing iterables without loading all of the iterable into memory at once. This makes chaining several generators very efficient.

Warning

Generators have one disadvantage over data structures like i.e. lists or tuples: They can be only processed once. Once the generator is exhausted the generator cannot be processed again.

Generators can be created in very different contexts, in this section these are grouped into three categories: processing an iterable, processing a value and from a function

Note

Several of the functions mentioned later do not return a generator in Python2. But there are equivalents in the itertools module. For example itertools.imap instead of map().

Processing several iterables

Note

These functions are implemented as methods for ManyIterables.

From a function