Skip to content

Instantly share code, notes, and snippets.

@viktorvillalobos
Created November 2, 2020 22:02
Show Gist options
  • Save viktorvillalobos/a39dd04c6df321a53d9d6c32dc02252d to your computer and use it in GitHub Desktop.
Save viktorvillalobos/a39dd04c6df321a53d9d6c32dc02252d to your computer and use it in GitHub Desktop.
Generate chunks using python generators.
# based on: https://stackoverflow.com/questions/24527006/split-a-generator-into-chunks-without-pre-walking-it
def chunks(iterable, size=10):
iterator = iter(iterable)
for first in iterator:
yield chain([first], islice(iterator, size - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment