I\'m trying to write the Haskel function \'splitEvery\' in Python. Here is it\'s definition:
splitEvery :: Int -> [e] -> [[e]]
@\'splitEvery\' n@ s
I came across this as I'm trying to chop up batches too, but doing it on a generator from a stream, so most of the solutions here aren't applicable, or don't work in python 3.
For people still stumbling upon this, here's a general solution using itertools:
from itertools import islice, chain
def iter_in_slices(iterator, size=None):
while True:
slice_iter = islice(iterator, size)
# If no first object this is how StopIteration is triggered
peek = next(slice_iter)
# Put the first object back and return slice
yield chain([peek], slice_iter)