class iteration_utilities.successive(iterable, times=2)

Like the recipe for pairwise but allows to get an arbitary number of successive elements.


iterable : iterable

Get the successive elements from this iterable.

times : int, optional

The number of successive elements. Default is 2.


successive_elements : generator

The successive elements as generator. Each element of the generator is a tuple containing times successive elements.


Each item of the iterable is returned as tuple with times successive items:

>>> from iteration_utilities import successive
>>> list(successive(range(5)))
[(0, 1), (1, 2), (2, 3), (3, 4)]

Varying the times can give you also 3 successive elements:

>>> list(successive(range(5), times=3))
[(0, 1, 2), (1, 2, 3), (2, 3, 4)]
>>> list(successive('Hello!', times=2))
[('H', 'e'), ('e', 'l'), ('l', 'l'), ('l', 'o'), ('o', '!')]

Tries to estimate for the length of the instance (returns 0 if an estimation is not possible).


Requires Python >= 3.4


(int) The number of successive items (readonly).

New in version 0.6.