sorting values in dict

Alex Martelli aleaxit at yahoo.com
Fri Jun 15 06:33:50 EDT 2001


"Victor Muslin" <victor at prodigy.net> wrote in message
news:3b292d23.1831931958 at localhost...
> This may not be the shortest or most efficient, but it does the trick:
>
> a={'a':9,'b':8,'c':7}
> l=[]
> for k,v in a.items():
>   l.append((v,k))
> l.sort()
> for v,k in l:
>   print v,k

I think it's pretty close, except that, to build l,
    l = [(v,k) for k,v in a.items()]
may be (very slightly) shorter and more efficient.


Alex






More information about the Python-list mailing list