[Python-Dev] PEP 265 - Sorting dicts by value
Nick Coghlan
ncoghlan at iinet.net.au
Mon Sep 13 13:46:47 CEST 2004
Moore, Paul wrote:
> From: Nick Coghlan
>
>>In Python 2.4:
>>
>>->>> ud = dict(a=1, b=2, c=3)
>>->>> from operator import itemgetter
>>->>> print sorted(ud.items(), key=itemgetter(1), reverse=True)
>>[('c', 3), ('b', 2), ('a', 1)]
>
>
> If you haven't done so already, I think this should be submitted
> to the cookbook. It's a nice idiom, and demonstrates some useful
> Python 2.4 features, and how they work well in combination.
It's submitted now.
I have a feeling Raymond is the one who should get the credit for the
approach, though. I'd be surprised if he made it through the discussions
about the introduction of sorted without using this example at least once :)
Cheers,
Nick.
More information about the Python-Dev
mailing list