key argument for max?

Steven Bethard steven.bethard at gmail.com
Thu Oct 21 12:28:33 EDT 2004


I was wondering if there's any plans to add a "key" argument to max
(and min) like was done for sort(ed)?  I fairly often run into a
situation where I have something like:

counts = {}
for item in iterable:
    counts[item] = counts.get(item, 0) + 1
_, max_item = max((count, item) for item, count in counts.items())

It would be nice to be able to write that last like like:

max(counts, key=counts.__getitem__)

If this seems like a good idea, maybe someone could point me in the
right direction and I could try to make a patch?  I've never looked at
the Python sources before, and it's been a long time since I've
written any C, but if it's not too hard...

Steve
-- 
You can wordify anything if you just verb it.
        - Bucky Katt, Get Fuzzy



More information about the Python-list mailing list