graviti.utility.itertools#

The implementation of iteration tools.

Module Contents#

Functions#

chunked(iterable, n)

Break an iterable instance into tuples of length n.

graviti.utility.itertools.chunked(iterable, n)[source]#

Break an iterable instance into tuples of length n.

Parameters
  • iterable (Iterable[_T]) – The input iterable instance which needs to be breaked into tuples of length n.

  • n (int) – The length of each yielded tuples.

Yields

The tuples of length n.

Return type

Iterator[Tuple[_T, Ellipsis]]

Examples

>>> list(chunked(range(9), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]

The last yielded tuple may have fewer than n items if the length of the input iterable instance is not divisible by n:

>>> list(chunked(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]