getting n items at a time from a generator

Paul Hankin paul.hankin at gmail.com
Thu Dec 27 07:07:52 EST 2007


On Dec 27, 11:34 am, Kugutsumen <kugutsu... at gmail.com> 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."""
>     chunk = []
>     count = 0
>     while True:
>         try:
>             item = items.next()
>             count += 1
>         except StopIteration:
>             yield chunk
>             break
>         chunk.append(item)
>         if not (count % size):
>             yield chunk
>             chunk = []
>             count = 0

The itertools module is always a good place to look when you've got a
complicated generator.

import itertools
import operator

def chunk(N, items):
    "Group items in chunks of N"
    def clump((n, _)):
        return n // N
    for _, group in itertools.groupby(enumerate(items), clump):
        yield itertools.imap(operator.itemgetter(1), group)

for ch in chunk(7, range(30)):
    print list(ch)


I've changed chunk to return a generator rather than building a list
which is probably only going to be iterated over. But if you prefer
the list version, replace 'itertools.imap' with 'map'.

--
Paul Hankin



More information about the Python-list mailing list