getting n items at a time from a generator

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Dec 27 07:24:29 EST 2007


On Thu, 27 Dec 2007 03:34:57 -0800, Kugutsumen wrote:

> I am relatively new the python language and I am afraid to be missing
> some clever construct or built-in way equivalent to my 'chunk' generator
> below.
> 
> def chunk(size, items):
>     """generate N items from a generator.""" 

[snip code]


Try this instead:


import itertools

def chunk(iterator, size): 
    # I prefer the argument order to be the reverse of yours.
    while True:
        chunk = list(itertools.islice(iterator, size))
        if chunk: yield chunk
        else: break


And in use:

>>> it = chunk(iter(xrange(30)), 7)
>>> for L in it:
...     print L
...
[0, 1, 2, 3, 4, 5, 6]
[7, 8, 9, 10, 11, 12, 13]
[14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27]
[28, 29]



-- 
Steven



More information about the Python-list mailing list