Skip to content

Instantly share code, notes, and snippets.

@kmerenkov
Created April 10, 2010 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kmerenkov/362375 to your computer and use it in GitHub Desktop.
Save kmerenkov/362375 to your computer and use it in GitHub Desktop.
from itertools import takewhile, islice, cycle
# chunkify(xrange(1, 11), 2) => [[1,2], [3,4], [5,6], [7,8], [9,10]]
def chunkify(iterable, chunk_size):
_iterable = iter(iterable)
return takewhile(lambda x: x,
(list(islice(_iterable, chunk_size)) for _ in cycle([True])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment