How to iterate the input over a particular size?
Peter Otten
__peter__ at web.de
Mon Dec 28 04:54:55 EST 2009
Francesco Bochicchio wrote:
> Not sure I understood your question, but if you need just to plit a
> big list in sublists of no more than 9 elements, then you can do
> someting like:
>
> def sublists(biglist, n ):
> "Splits a big list in sublists of max n elements"
> prev_idx = 0; res = []
> for idx in xrange(n, len(biglist)+n, n ):
> res.append( biglist[prev_idx:idx] )
> prev_idx = idx
> return res
>
> I would not be surprised if somewhere in python standard library there
> is something like this (possibly better), but
> could not find anything.
A lazy alternative:
>>> from itertools import islice
>>> def chunks(items, n):
... items = iter(items)
... for first in items:
... chunk = [first]
... chunk.extend(islice(items, n-1))
... yield chunk
...
>>> list(chunks(range(10), 3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
One with lazy chunks:
>>> from itertools import chain, islice
>>> def chunks(items, n):
... items = iter(items)
... for first in items:
... yield chain((first,), islice(items, n-1))
...
>>> [list(chunk) for chunk in chunks(range(10), 3)]
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
Prior art by Duncan Booth:
http://mail.python.org/pipermail/python-list/2007-November/517993.html
Peter
More information about the Python-list
mailing list