groupby behaviour

andrea crotti andrea.crotti.0 at
Tue Feb 26 18:09:24 CET 2013

2013/2/26 Ian Kelly <ian.g.kelly at>:
> On Tue, Feb 26, 2013 at 9:27 AM, andrea crotti
> <andrea.crotti.0 at> wrote:
>> So I was trying to use groupby (which I used in the past), but I
>> noticed a very strange thing if using list on
>> the result:
> As stated in the docs:
> """
> The returned group is itself an iterator that shares the underlying
> iterable with groupby(). Because the source is shared, when the
> groupby() object is advanced, the previous group is no longer visible.
> So, if that data is needed later, it should be stored as a list:
> """
> --

I should have read more carefully sorry, I was in the funny situation
where it would have actually worked in the production code but it was
failing in the unit tests (because I was using list only there).

It's very weird though this sharing and still doesn't really look
rightl, is it done just for performance reasons?

More information about the Python-list mailing list