[Python-ideas] Add a .chunks() method to sequences

Geoffrey Spear geoffspear at gmail.com
Tue May 2 08:33:17 EDT 2017


On Tue, May 2, 2017 at 8:10 AM <robert.hoelzl at posteo.de> wrote:

> Steven D’Aprano was giving me an idea (in the bytes.hex delimiter
> discussion):
>
>
>
> I had very often the use case that I want to split sequences into
> subsequences of same size.
>
> How about adding a chunks() and rchunks() function to sequences:
>
>
>
> [1,2,3,4,5,6,7].chunks(3) => [[1,2,3], [4,5,6], [7]]
>
> "1234“.chunks(2) => [“12“, “34“]
>
>
>
> (this could then be used to emulate stevens proposal: “
> “.join(“1234567“.chunks(2)) => “12 34 56 7“)
>
>
>

Changing the definition of the Sequence ABC to avoid needing to use a
2-line function from the itertools recipes seems like a pretty drastic
change. I don't think there's even a compelling argument for adding
grouper() to itertools, let along to every single sequence.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170502/ea46f824/attachment.html>


More information about the Python-ideas mailing list