Created
December 6, 2017 10:26
-
-
Save nwjlyons/621fabfc0d4c1119b2ad338f615ce4ef to your computer and use it in GitHub Desktop.
Python split generator into sub lists
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
def chunks(generator, chunk_size): | |
"""Yield successive chunks from a generator""" | |
chunk = [] | |
for item in generator: | |
if len(chunk) >= chunk_size: | |
yield chunk | |
chunk = [item] | |
else: | |
chunk.append(item) | |
if chunk: | |
yield chunk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
eg.