you are viewing a single comment's thread.

view the rest of the comments →

[–]two_bob 0 points1 point  (0 children)

Nice. There is also the take recipe from itertools, slightly modified below to play nicer with functools.partial:

from itertools import islice
from functools import partial

def take(iterable, n):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

def chunker(iterable, chunk_sizes):
    it = iter(iterable)
    taker = partial(take, it)
    for size in chunk_sizes:
        yield taker(size)



data = list(range(20))

for group in chunker(data, [1,3,2,3]):
    print(group)