Iterate over a python sequence in multiples of n?
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