How to iterate through a sequence, grabbing subsequences?

George Sakkis george.sakkis at gmail.com
Fri Sep 29 11:00:28 EDT 2006


Matthew Wilson wrote:

> I wrote a function that I suspect may already exist as a python builtin,
> but I can't find it:
>
> 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?

There's not any builtin for this, but the same topic came up just three
days ago: http://tinyurl.com/qec2p. 

Regards,
George




More information about the Python-list mailing list