Last active
September 18, 2018 18:47
-
-
Save smoofra/3a3e8fee2dc7433f82b481e694fd97d0 to your computer and use it in GitHub Desktop.
chunks
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(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