sorting a dictionary

Andrew Bennetts andrew-pythonlist at puzzling.org
Tue Feb 4 12:05:24 CET 2003


On Tue, Feb 04, 2003 at 10:43:09AM +0000, Alex Martelli wrote:
> Andrew Bennetts wrote:
>    ...
> > Sure you can... what's wrong with the solution I posted earlier:
> > 
> >     max([(x[1], x[0]) for x in d.items()])[1]
> 
> This may fail if the dictionary has a complex-number key:

Heh.  That's a sneaky requirement -- I didn't think of that :)

> You can fix that in various ways, e.g.:
> 
> max([(d[k], i, k) for k, i in zip(d,range(len(d))) ])[-1]
> 
> but I think this is a bit too complicated for a 1-liner (your

Indeed :)

-Andrew.






More information about the Python-list mailing list