how to sort a dictionary by the values ?

June Kim junaftnoon at nospamplzyahoo.com
Sat Dec 30 07:16:13 EST 2000


"hwan-jo yu" <hwanjoyu at students.uiuc.edu> wrote in message
news:Pine.GSO.4.10.10012292313350.21787-100000 at ux11.cso.uiuc.edu...
> Hi,
> If I sort the items() of dictionary, it is sorted by first the key and
> second the value.
> How can I sort it by the values ?
> Thanks in advance.
>
>

There are several ways but one of the simplest, not the fastest, would be:
>>> dic={ ... }
>>> z=dic.items()
>>> z.sort(lambda x,y:cmp(x[1],y[1]))

Best regards,

June




More information about the Python-list mailing list