itertools.groupby

Paul Rubin http
Mon May 28 17:31:22 EDT 2007


Gordon Airporte <JHoover at fbi.gov> writes:
> This is my first exposure to this function, and I see that it does
> have some uses in my code. I agree that it is confusing, however.
> IMO the confusion could be lessened if the function with the current
> behavior were renamed 'telescope' or 'compact' or 'collapse' or
> something (since it collapses the iterable linearly over homogeneous
> sequences.)

It chops up the iterable into a bunch of smaller ones, but the total
size ends up the same.  "Telescope", "compact", "collapse" etc. make
it sound like the output is going to end up smaller than the input.

There is also a dirty secret involved <wink>, which is that the
itertools functions (including groupby) are mostly patterned after
similarly named functions in the Haskell Prelude, which do about the
same thing.  They are aimed at helping a similar style of programming,
so staying with similar names IMO is a good thing.

> A function named groupby could then have what I think is the clearly
> implied behavior of creating just one iterator for each unique type of
> thing in the input list, as categorized by the key function.

But that is what groupby does, except its notion of uniqueness is
limited to contiguous runs of elements having the same key.



More information about the Python-list mailing list