[Tutor] Chunking list/array data?
Peter Otten
__peter__ at web.de
Thu Aug 22 05:58:05 EDT 2019
Sarah Hembree wrote:
> How do you chunk data? We came up with the below snippet. It works (with
> integer list data) for our needs, but it seems so clunky.
>
> def _chunks(lst: list, size: int) -> list:
> return [lst[x:x+size] for x in range(0, len(lst), size)]
>
> What do you do? Also, what about doing this lazily so as to keep memory
> drag at a minimum?
If you don't mind filling up the last chunk with dummy values this will
generate tuples on demand from an arbitrary iterable:
>>> from itertools import zip_longest
>>> def chunks(items, n):
... return zip_longest(*[iter(items)]*n)
...
>>> chunked = chunks("abcdefgh", 3)
>>> next(chunked)
('a', 'b', 'c')
>>> list(chunked)
[('d', 'e', 'f'), ('g', 'h', None)]
More information about the Tutor
mailing list