Created
November 8, 2020 02:41
-
-
Save yanniskatsaros/2564a65b8238157e6fb1c68b44eb0f66 to your computer and use it in GitHub Desktop.
Split an iterable into n 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
from itertools import islice | |
def itersplit(iterable, n): | |
i = len(iterable) % n | |
start = 0; stop = i | |
for _ in range(n): | |
yield list(islice(iterable, start, stop)) | |
start += i; stop += i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment