Last active
June 13, 2016 02:09
-
-
Save ruda/5239383bb84aaf1abf1a312717b633fa to your computer and use it in GitHub Desktop.
Split iterable in group of 'n' values and yield these values.
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 nsplit(iterable, n): | |
result = [] | |
for i, x in enumerate(iterable, 1): | |
result.append(x) | |
if i % n == 0: | |
yield result | |
result = [] | |
if result: | |
yield result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment