itertools.groupby

Carsten Haese carsten at uniqsys.com
Tue May 29 08:39:29 EDT 2007


On Mon, 2007-05-28 at 23:34 -0700, Raymond Hettinger wrote:
> On May 28, 8:36 pm, "Carsten Haese" <cars... at uniqsys.com> wrote:
> > And while
> > we're at it, it probably should be keyfunc(value), not key(value).
> 
> No dice.  The itertools.groupby() function is typically used
> in conjunction with sorted().  It would be a mistake to call
> it keyfunc in one place and not in the other.  The mental
> association is essential.  The key= nomenclature is used
> throughout Python -- see min(), max(), sorted(), list.sort(),
> itertools.groupby(), heapq.nsmallest(), and heapq.nlargest().

Point taken, but in that case, the argument name in the function
signature is technically incorrect. I don't really need this corrected,
I was merely pointing out the discrepancy between the name 'keyfunc' in
the signature and the call 'key(value)' in the description. For what
it's worth, which is probably very little, help(sorted) correctly
identifies the name of the key argument as 'key'.

As an aside, while groupby() will indeed often be used in conjunction
with sorted(), there is a significant class of use cases where that's
not the case: I use groupby to produce grouped reports from the results
of an SQL query. In such cases, I use ORDER BY to guarantee that the
results are supplied in the correct order rather than using sorted().

Having said that, I'd like to expressly thank you for providing such a
mindbogglingly useful feature. Writing reports would be much less
enjoyable without groupby.

Best regards,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list