groupby behaviour

Paul Rubin no.email at nospam.invalid
Tue Feb 26 12:35:22 EST 2013


andrea crotti <andrea.crotti.0 at gmail.com> writes:
> It's very weird though this sharing and still doesn't really look
> rightl, is it done just for performance reasons?

It could consume unbounded amounts of memory otherwise.  E.g. if there
are millions of items in the group.

If you're not worried about that situation it's simplest to just
convert each group to a list for processing it:

   for k,g in groupby(...):
      gs = list(g)
      # do stuff with gs

calling list(g) reads all the elements in the group, advancing groupby's
internal iterator to the next group.  Since you've copied out all the
group elements, you don't have to worry about the sharing effect.



More information about the Python-list mailing list