Skip to content

Instantly share code, notes, and snippets.

@yanniskatsaros
Created November 8, 2020 02:41
Show Gist options
  • Save yanniskatsaros/2564a65b8238157e6fb1c68b44eb0f66 to your computer and use it in GitHub Desktop.
Save yanniskatsaros/2564a65b8238157e6fb1c68b44eb0f66 to your computer and use it in GitHub Desktop.
Split an iterable into n chunks using Python generators
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