sorting a dictionary

Alex Martelli aleax at aleax.it
Tue Feb 4 11:45:02 CET 2003


Harvey Thomas wrote:
   ..
>  max([(v,k) for k,v in d.items()])[1]
> 
> which I think does the required job O(N)?

Yes -- indeed I was wrong about the O(N) part -- your code
has a potential subtle bug (may compare keys thus crash
when one of equal values has a complex key, see my reply
to A. Bennett), but I've already posted several ways to
get solid O(N) solutions to atone for my error;-).


Alex





More information about the Python-list mailing list