Skip to content

Instantly share code, notes, and snippets.

@lukmdo
lukmdo / chunk_iterable.py
Created October 24, 2011 14:45 — forked from ksamuel/chunk_iterable.py
Function to iterate over an iterable chunk by chunk, choosing the number of item of each chunks and the type of the chunk
from itertools import chain, islice
def chunk(seq, chunksize):
""" Yields items from an iterator in iterable chunks."""
it = iter(seq)
while True:
yield chain([it.next()], islice(it, chunksize - 1))
if __name__ == '__main__':
lst = ['a', 'b', 'c', 'd', 'e']