6.5 itertools -- Functions creating iterators for efficient looping
This module implements a number of iterator building blocks inspired by constructs from the Haskell and SML programming languages. Each has been recast in a form suitable for Python.
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Standardization helps avoid the readability and reliability problems which arise when many different individuals create their own slightly varying implementations, each with their own quirks and naming conventions.
The tools are designed to combine readily with one another. This makes it easy to construct more specialized tools succinctly and efficiently in pure Python.
For instance, SML provides a tabulation tool: tabulate(f)
which produces a sequence f(0), f(1), ...
. This toolbox
provides imap() and count() which can be combined
to form imap(f, count())
and produce an equivalent result.
Likewise, the functional tools are designed to work well with the high-speed functions provided by the operator module.
The module author welcomes suggestions for other basic building blocks to be added to future versions of the module.
Whether cast in pure python form or compiled code, tools that use iterators are more memory efficient (and faster) than their list based counterparts. Adopting the principles of just-in-time manufacturing, they create data when and where needed instead of consuming memory with the computer equivalent of ``inventory''.
The performance advantage of iterators becomes more acute as the number of elements increases - at some point, lists grow large enough to severely impact memory cache performance and start running slowly.
See Also:
See About this document... for information on suggesting changes.