Newbie problem

Thomas Guettler zopestoller at thomas-guettler.de
Tue Dec 17 07:36:26 EST 2002


Teemu Lahti schrieb:
> I have this very beginner problem. I need to sort dictionary by its values.
> I know how to do it by its keys, but I have no idea how to do it with its
> values. Any ideas? :)

Hi Teel!

Here is one solution:

d={"a": "one",
    "b": "two",
    "c": "three",
    "d": "four",
    "e": "five"}

items=d.items()
print items
def value_cmp(a, b):
     return cmp(a[1], b[1])
items.sort(value_cmp)
print items

  thomas




More information about the Python-list mailing list