how to split an iterable in constant-size chunks
This is probably more efficient (faster)
def batch(iterable, n=1): l = len(iterable) for ndx in range(0, l, n): yield iterable[ndx:min(ndx + n, l)]for x in batch(range(0, 10), 3): print x
Example using list
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # list of data for x in batch(data, 3): print(x)# Output[0, 1, 2][3, 4, 5][6, 7, 8][9, 10]
It avoids building new lists.
FWIW, the recipes in the itertools module provides this example:
def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return zip_longest(fillvalue=fillvalue, *args)
It works like this:
>>> list(grouper(3, range(10)))[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
As others have noted, the code you have given does exactly what you want. For another approach using itertools.islice
you could see an example of following recipe:
from itertools import islice, chaindef batch(iterable, size): sourceiter = iter(iterable) while True: batchiter = islice(sourceiter, size) yield chain([batchiter.next()], batchiter)