chunked¶
- emlib.iterlib.chunked(iterable, n)[source]¶
Break iterable into lists of length n:
>>> list(chunked([1, 2, 3, 4, 5, 6], 3)) [[1, 2, 3], [4, 5, 6]]
By the default, the last yielded list will have fewer than n elements if the length of iterable is not divisible by n:
>>> list(chunked([1, 2, 3, 4, 5, 6, 7, 8], 3)) [[1, 2, 3], [4, 5, 6], [7, 8]]
To use a fill-in value instead, see the
grouper()
recipe.If the length of iterable is not divisible by n and strict is
True
, thenValueError
will be raised before the last list is yielded.