tensorbay.utility.itertools#

The implementation of iteration tools.

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

Break an iterable instance into tuples of length n.

Parameters
  • iterable (Iterable[tensorbay.utility.itertools._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[tensorbay.utility.itertools._T, …]]

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,)]