successive

class iteration_utilities.successive(iterable, times=2)

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

Parameters:
iterableiterable

Get the successive elements from this iterable.

timesint, optional

The number of successive elements. Default is 2.

Returns:
successive_elementsgenerator

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

Examples

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', '!')]
__length_hint__()

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

times

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

New in version 0.6.