How do I process the elements of a sequence in batches, idiomatically?
For example, with the sequence \"abcdef\" and a batch size of 2, I would like to do something
you can create the following generator
def chunks(seq, size): a = range(0, len(seq), size) b = range(size, len(seq) + 1, size) for i, j in zip(a, b): yield seq[i:j]
and use it like this:
for i in chunks('abcdef', 2): print(i)