Last active

Function to iterate over an iterable chunk by chunk, choosing the number of item of each chunks and the type of the chunk

  • Download Gist
1 2 3 4 5 6 7 8 9 10 11 12
from itertools import chain, islice
def chunk(seq, chunksize, process=iter):
""" Yields items from an iterator in iterable chunks."""
it = iter(seq)
while True:
yield process(chain([], islice(it, chunksize - 1)))
if __name__ == '__main__':
lst = ['a', 'b', 'c', 'd', 'e', 'f']
for i in chunk(lst, 2, list):
print i

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.