[Python-ideas] list.sort with a int or str key

Terry Reedy tjreedy at udel.edu
Fri Sep 17 05:11:22 CEST 2010


On 9/16/2010 2:28 PM, Raymond Hettinger wrote:

> The key= parameter is a protocol that is used across multiple tools min(). max(), groupby(), nmallest(), nlargest(), etc.  All of those would need to change to stay in-sync.
...

> ISTM, the performance would be about the same as you already get from attrgetter(), itemgetter(), and methodcaller().  Also, those three tools are already more flexible than the proposal, for example:
>
>  attrgetter('lastname', 'firstname')  # key = lambda r: (r.lastname, r.firstname)
>  itemgetter(0, 7)                     # key = lambda r: (r[0], r[7])
>  methodcaller('get_stats', 'size')    # key = lambda r: r.get_stats('size')

It is easy to not know about these. I think the doc set could usefully 
use an expanded entry on *key functions* (that would be a 
cross-reference link) that includes examples like the above. Currently, 
for example, the min entry has "The optional keyword-only key argument 
specifies a one-argument ordering function like that used for 
list.sort()." but there is no link and going to list.sort only adds 
"that is used to extract a comparison key from each list element: 
key=str.lower. The default value is None." Perhaps we could expand that 
and make the existing cross-references into links.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list