[Python-ideas] itertools.chunks()

anatoly techtonik techtonik at gmail.com
Sun Apr 28 12:22:56 CEST 2013


On Sat, Apr 6, 2013 at 3:50 PM, Giampaolo Rodolà <g.rodola at gmail.com> wrote:

> def chunks(total, step):
>     assert total >= step
>     while total > step:
>         yield step;
>         total -= step;
>     if total:
>         yield total
>
> >>> chunks(12, 4)
> [4, 4, 4]
> >>> chunks(13, 4)
> [4, 4, 4, 1]
>
>
> I'm not sure how appropriate "chunks" is as a name for such a function.
>

This name is better to be reserved for chunking actual data rather than
indexes:
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python


> Now I wonder, would it make sense to have something like this into
> itertools module?
>
-- 
anatoly t.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130428/31fac306/attachment.html>


More information about the Python-ideas mailing list