Skip to content

Instantly share code, notes, and snippets.

@arc279
Last active April 26, 2018 08:18
Show Gist options
  • Save arc279/ac9783043c1921580f34facd3f2d9599 to your computer and use it in GitHub Desktop.
Save arc279/ac9783043c1921580f34facd3f2d9599 to your computer and use it in GitHub Desktop.
import itertools
def each_slice(iterable, n):
for i, item in itertools.groupby(enumerate(iterable), lambda x: x[0] // n):
yield (x[1] for x in item)
for x in each_slice(range(10), 3):
print(tuple(x))
"""output
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9,)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment