See the "grouper" example in http://docs.python.org/library/itertools.html<br><br>On Friday, August 31, 2012 11:28:33 PM UTC-7, anatoly techtonik wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">I've run into the necessity of implementing chunks() again. Here is
<br>the code I've made from scratch.
<br>
<br>def chunks(seq, size):
<br>  '''Cut sequence into chunks of given size. If `seq` length is
<br>     not divisible by `size` without reminder, last chunk will
<br>     have length less than size.
<br>
<br>     >>> list( chunks([1,2,3,4,5,6,7], 3) )
<br>     [[1, 2, 3], [4, 5, 6], [7]]
<br>  '''
<br>  endlen = len(seq)//size
<br>  for i in range(endlen):
<br>    yield [seq[i*size+n] for n in range(size)]
<br>  if len(seq) % size:
<br>    yield seq[endlen*size:]
<br>
<br>--
<br>anatoly t.
<br>
<br>
<br>On Fri, Jun 29, 2012 at 11:32 PM, Georg Brandl <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="rSPDnwtDkwQJ">g.br...@gmx.net</a>> wrote:
<br>> On 26.06.2012 10:03, anatoly techtonik wrote:
<br>>>
<br>>> Now that Python 3 is all about iterators (which is a user killer
<br>>> feature for Python according to StackOverflow -
<br>>> <a href="http://stackoverflow.com/questions/tagged/python" target="_blank">http://stackoverflow.com/<wbr>questions/tagged/python</a>) would it be nice to
<br>>> introduce more first class functions to work with them? One function
<br>>> to be exact to split string into chunks.
<br>>>
<br>>>      itertools.chunks(iterable, size, fill=None)
<br>>>
<br>>> Which is the 33th most voted Python question on SO -
<br>>>
<br>>> <a href="http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312464" target="_blank">http://stackoverflow.com/<wbr>questions/312443/how-do-you-<wbr>split-a-list-into-evenly-<wbr>sized-chunks-in-python/312464</a>
<br>>>
<br>>> P.S. CC'ing to python-dev@ to notify about the thread in python-ideas.
<br>>>
<br>>
<br>> Anatoly, so far there were no negative votes -- would you care to go
<br>> another step and propose a patch?
<br>>
<br>>
<br>> Georg
<br>>
<br>> ______________________________<wbr>_________________
<br>> Python-ideas mailing list
<br>> <a href="javascript:" target="_blank" gdf-obfuscated-mailto="rSPDnwtDkwQJ">Python...@python.org</a>
<br>> <a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="rSPDnwtDkwQJ">Python...@python.org</a>
<br><a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br></blockquote>