+

itertools in Python

Provides a set of tools for creating iterators.

The module provides fast, memory efficient tools that create iterators . These tools, inspired by constructs from APL, Haskell, SML, can be used individually or in various combinations. For example, SML has a tabulate (f) that generates a sequence f (0), f (1), ... , which in Python can be rendered as a map (f, count ()) .

Notes

The tools provided by the module work well with the functions of the operator module. For example, the multiplication operator can be applied to two vectors to efficiently compute the dot product: sum (map (operator.mul, vector1, vector2)) .


categories:

  • Infinite iterators: count () , cycle () , repeat () . Provides a stream of unlimited length, so use it inside functions or loops that stop the stream.
  • Final iterators: accumulate () , chain () , < code> chain.from_iterable () , compress () , dropwhile () , filterfalse () , groupby ( ) , islice () , starmap () , takewhile () , tee () , zip_longest().
  • Iterators for combinatorics: product () , permutations () , combinations () , combinations_with_replacement().

Notes

More primitives offering iterators to solve a set tasks can be taken from the package more-itertools . Primitives are also effective in minimizing memory consumption and speed.


Get Solution for free from DataCamp guru