Skip to content

Instantly share code, notes, and snippets.

@smoofra
Last active September 18, 2018 18:47
Show Gist options
  • Save smoofra/3a3e8fee2dc7433f82b481e694fd97d0 to your computer and use it in GitHub Desktop.
Save smoofra/3a3e8fee2dc7433f82b481e694fd97d0 to your computer and use it in GitHub Desktop.
chunks
def chunks(iterable, n):
"""group elements of iterable into chunks of size n and yield an iterator for
each chunk"""
i = iter(iterable)
def groupiter(first):
yield first
for x in range(n-1):
try:
yield next(i)
except StopIteration:
break
for item in i:
yield(groupiter(item))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment