[Python-Dev] [Python-ideas] itertools.chunks(iterable, size, fill=None)

anatoly techtonik techtonik at gmail.com
Wed Jul 4 11:57:20 CEST 2012

On Fri, Jun 29, 2012 at 11:32 PM, Georg Brandl <g.brandl at gmx.net> wrote:
> On 26.06.2012 10:03, anatoly techtonik wrote:
>> Now that Python 3 is all about iterators (which is a user killer
>> feature for Python according to StackOverflow -
>> http://stackoverflow.com/questions/tagged/python) would it be nice to
>> introduce more first class functions to work with them? One function
>> to be exact to split string into chunks.
>>      itertools.chunks(iterable, size, fill=None)
>> Which is the 33th most voted Python question on SO -
>> http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312464
>> P.S. CC'ing to python-dev@ to notify about the thread in python-ideas.
> Anatoly, so far there were no negative votes -- would you care to go
> another step and propose a patch?

Was about to say "no problem", but in fact - there is. Sorry from
whining from my side and thanks for nudging. The only thought that a
simple task of copy/pasting relevant code from
will require a few hours waiting of download (still not everybody has
a high-speed internet) makes me switch to other less time consuming
tasks before getting around to it. These tasks become more important
in a few hours, and basically I've passed through this many times
before. It then becomes quite hard to switch back.

I absolutely don't mind someone else being credited for the idea,
because ideas usually worthless without implementation. It will be
interesting to design how the process could work in a separate thread.
For now the best thing I can do (I don't risk even to mention anything
with 3.3) is to copy/paste code from the docs here:

from itertools import izip_longest
def chunks(iterable, size, fill=None):
    """Split an iterable into blocks of fixed-length"""
    # chunks('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * size
    return izip_longest(fillvalue=fill, *args)

BTW, this doesn't work as expected (at least for strings). Expected is:
  chunks('ABCDEFG', 3, 'x') --> 'ABC' 'DEF' 'Gxx'
  chunks('ABCDEFG', 3, 'x') --> ('A' 'B' 'C') ('D' 'E' 'F') ('G' 'x' 'x')

Needs more round tuits definitely.

More information about the Python-Dev mailing list