iterate_sequence_nwise

sequencetools.iterate_sequence_nwise(iterable, n=2, cyclic=False, wrapped=False)

Iterates elements in iterable n at a time.

Iterates iterable by pairs:

>>> for x in sequencetools.iterate_sequence_nwise(range(10)):
...     x
...
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 7)
(7, 8)
(8, 9)

Iterates iterable by triples:

>>> for x in sequencetools.iterate_sequence_nwise(range(10), n=3):
...     x
...
(0, 1, 2)
(1, 2, 3)
(2, 3, 4)
(3, 4, 5)
(4, 5, 6)
(5, 6, 7)
(6, 7, 8)
(7, 8, 9)

Iterates iterable by pairs. Wraps around at end:

>>> for x in sequencetools.iterate_sequence_nwise(
...     range(10), n=2, wrapped=True):
...     x
...
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 7)
(7, 8)
(8, 9)
(9, 0)

Iterates iterable by triples. Wraps around at end:

>>> for x in sequencetools.iterate_sequence_nwise(
...     range(10), n=3, wrapped=True):
...     x
...
(0, 1, 2)
(1, 2, 3)
(2, 3, 4)
(3, 4, 5)
(4, 5, 6)
(5, 6, 7)
(6, 7, 8)
(7, 8, 9)
(8, 9, 0)
(9, 0, 1)

Iterates iterable by pairs. Cycles indefinitely:

>>> pairs = sequencetools.iterate_sequence_nwise(
...     range(10), n=2, cyclic=True)
>>> for _ in range(15):
...     next(pairs)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 7)
(7, 8)
(8, 9)
(9, 0)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)

Returns infinite generator.

Iterates iterable by triples. Cycles indefinitely:

>>> triples = sequencetools.iterate_sequence_nwise(
...     range(10), n=3, cyclic=True)
...
>>> for _ in range(15):
...     next(triples)
(0, 1, 2)
(1, 2, 3)
(2, 3, 4)
(3, 4, 5)
(4, 5, 6)
(5, 6, 7)
(6, 7, 8)
(7, 8, 9)
(8, 9, 0)
(9, 0, 1)
(0, 1, 2)
(1, 2, 3)
(2, 3, 4)
(3, 4, 5)
(4, 5, 6)

Returns infinite generator.

Ignores wrapped when cyclic=True.

Returns generator.