Created
November 2, 2020 22:02
-
-
Save viktorvillalobos/a39dd04c6df321a53d9d6c32dc02252d to your computer and use it in GitHub Desktop.
Generate chunks using python generators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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