Itertools

This part of Dooku contains various iteration algorithms that are missed in Python’s standard library and here they are:

chunk_by

dooku.itertools.chunk_by(n, iterable, fillvalue=None)

Iterate over a given iterable by n elements at a time.

>>> for x, y in chunk_by(2, [1, 2, 3, 4, 5]):
... # iteration no 1: x=1, y=2
... # iteration no 2: x=3, y=4
... # iteration no 3: x=5, y=None
Parameters:
  • n – (int) a chunk size number
  • iterable – (iterator) an input iterator
  • fillvalue – (any) a value to be used to fit chunk size if there not enough values in input iterator
Returns:

(iterator) an output iterator that iterates over the input one by chunks of size n