[Python-ideas] Looking for a "batch" function

Chris Rebert pyideas at rebertia.com
Sat Jul 17 10:52:59 CEST 2010


On Sat, Jul 17, 2010 at 1:36 AM, Shane Hathaway <shane at hathawaymix.org> wrote:
> Hi all,
>
> An operation I often want in my Python code is some kind of simple batch
> function.  The batch function would take an iterator and return same-size
> batches from it, except the last batch, which could be smaller.  Two
> parameters would be required: the iterator and the size of each batch.  Here
> are some examples of what I would expect this batch function to do.
>
> Get batches from a list:
>
>>>> list(batch([1,2,3,4,5], 2))
> [[1, 2], [3, 4], [5]]
>
> Get batches from a string:
>
>>>> list(batch('one two six ten', 4))
> ['one ', 'two ', 'six ', 'ten']
>
> Organize a stream of objects into a table:
>
>>>> list(batch(['Somewhere', 'CA', 90210, 'New York', 'NY', 10001], 3))
> [['Somewhere', 'CA', 90210], ['New York', 'NY', 10001]]
>
> My intuition tells me that such a function should exist in Python, but I
> have not found it in the builtin functions, slice operators, or itertools.
>  Did I miss it?

IMO, yes.

> Obviously, I can just include that function in my projects, but I wonder if
> there is some built-in version of it.  If there isn't, maybe there should
> be.

See the "grouper" recipe in itertools:
http://docs.python.org/library/itertools.html#recipes
It does almost exactly what you want:
grouper(3, 'ABCDEFG', 'x') --> ['A','B','C'], ['D','E','F'], ['G','x','x']

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-ideas mailing list