Iterate over a python sequence in multiples of n?
I've got an alternative approach, that works for iterables that don't have a known length.
def groupsgen(seq, size):
it = iter(seq)
while True:
values = ()
for n in xrange(size):
values += (it.next(),)
yield values
It works by iterating over the sequence (or other iterator) in groups of size, collecting the values in a tuple. At the end of each group, it yield the tuple.
When the iterator runs out of values, it produces a StopIteration exception which is then propagated up, indicating that groupsgen is out of values.
It assumes that the values come in sets of size (sets of 2, 3, etc). If not, any values left over are just discarded.
A generator function would be neat:
def batch_gen(data, batch_size):
for i in range(0, len(data), batch_size):
yield data[i:i+batch_size]
Example use:
a = "abcdef"
for i in batch_gen(a, 2): print i
prints:
ab
cd
ef