itertools --- Functions creating iterators for efficient looping

synopsis: Functions creating iterators for efficient looping.

from itertools import import collections import math import operator import random

This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. 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. Together, they form an "iterator algebra" making it possible to construct 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), .... The same effect can be achieved in Python by combining map and count to form map(f, count()).

============================ ============================ ================================================= ============================================================= Iterator Arguments Results Example ============================ ============================ ================================================= ============================================================= accumulate p [,func] p0, p0+p1, p0+p1+p2, ... accumulate([1,2,3,4,5]) → 1 3 6 10 15 batched p, n (p0, p1, ..., p_n-1), ... batched('ABCDEFG', n=3) → ABC DEF G chain p, q, ... p0, p1, ... plast, q0, q1, ... chain('ABC', 'DEF') → A B C D E F chain.from_iterable iterable p0, p1, ... plast, q0, q1, ... chain.from_iterable(['ABC', 'DEF']) → A B C D E F compress data, selectors (d[0] if s[0]), (d[1] if s[1]), ... compress('ABCDEF', [1,0,1,0,1,1]) → A C E F count [start[, step]] start, start+step, start+2step, ... count(10) → 10 11 12 13 14 ... cycle p p0, p1, ... plast, p0, p1, ... cycle('ABCD') → A B C D A B C D ... dropwhile predicate, seq seq[n], seq[n+1], starting when predicate fails dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8 filterfalse predicate, seq elements of seq where predicate(elem) fails filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8 groupby iterable[, key] sub-iterators grouped by value of key(v) groupby(['A','B','DEF'], len) → (1, A B) (3, DEF) islice seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) → C D E F G pairwise iterable (p[0], p[1]), (p[1], p[2]) pairwise('ABCDEFG') → AB BC CD DE EF FG repeat elem [,n] elem, elem, elem, ... …