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


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to