Simple batch function for Python
Often I have an iterable i want to group. For example a list of integers and i want to process two at once. That’s a pretty nice idom I found in the documentation translated to itertools:
from itertools import izip, repeat
def batch(iterable, n):
return izip(*repeat(iter(iterable), n))
Use it like that:
>>> for key, value in batch([1, 2, 3, 4], 2):
... print key, value
...
1 2
3 4
This only works if the sequence you’re processing is cleanly divisible by the batch size, or if you never want to process any stragglers. For example, if you do batch([1,2,3,4,5],2), it’ll never handle the 5.
Comment by Mike Pirnat — Wednesday, May 7th, 2008 @ 1:13 pmYes. But that’s the most common use case. (i)zip drops extra items in the list too.
Comment by Armin Ronacher — Wednesday, May 7th, 2008 @ 10:13 pmI once wrote something similar, called it chunkize. There were two versions: one dropping dangling parts (in one LC), one preserving them.
Pretty useful, I find myself needing this every now and then.
Comment by Marek Kubica — Wednesday, May 7th, 2008 @ 10:26 pm