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.
Processing an iterable¶
itertools.chain.from_iterable()
(implemented as flatten inIterable
)combinations_from_relations()
(not implemented inIterable
)itertools.groupby()
(not implemented inIterable
)ipartition()
(not implemented inIterable
)sideeffects()
(not implemented inIterable
)itertools.tee()
(not implemented inIterable
)tee_lookahead()
(not implemented inIterable
)
Processing several iterables¶
Note
These functions are implemented as methods for
ManyIterables
.
Processing a value¶
From a function¶
iter()
(two arguments) (implemented as from_iterfunc_sentinel inIterable
)iter_except()
(implemented as from_iterfunc_exception inIterable
)