How to iterate through a sequence, grabbing subsequences?

Tim Chase python.list at
Fri Sep 29 17:01:56 CEST 2006

> def chunkify(s, chunksize):
>     "Yield sequence s in chunks of size chunksize."
>     for i in range(0, len(s), chunksize):
>         yield s[i:i+chunksize]
> I wrote this because I need to take a string of a really, really long
> length and process 4000 bytes at a time.
> Is there a better solution?

My first thought, if len(s) is truely huge, would be to replace 
range() with xrange() so that you don't build a list of 
len(s)/chunksize elements, just to throw it away.

However, I think that's about as good as this common idiom gets.

I've seen variants which will always yield portions of chunksize 
in size, padding it out to the proper length, but that's a 
specification issue that you don't seem to want/need.


More information about the Python-list mailing list