[Python-Dev] [Python-ideas] itertools.chunks(iterable, size, fill=None)
anatoly techtonik
techtonik at gmail.com
Sat Sep 1 08:27:25 CEST 2012
I've run into the necessity of implementing chunks() again. Here is
the code I've made from scratch.
def chunks(seq, size):
'''Cut sequence into chunks of given size. If `seq` length is
not divisible by `size` without reminder, last chunk will
have length less than size.
>>> list( chunks([1,2,3,4,5,6,7], 3) )
[[1, 2, 3], [4, 5, 6], [7]]
'''
endlen = len(seq)//size
for i in range(endlen):
yield [seq[i*size+n] for n in range(size)]
if len(seq) % size:
yield seq[endlen*size:]
--
anatoly t.
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?
>
>
> Georg
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
More information about the Python-Dev
mailing list