Is it possible to sort a dictionary?

Alex Martelli aleaxit at yahoo.com
Mon Mar 26 14:22:13 EST 2001


"Dave LeBlanc" <whisper at oz.net> wrote in message
news:99o33k$aq4$0 at 216.39.170.247...
> How about:
>
> # prints the elements of myDictionary in sort order
> # error handling needed (myKeys != none for example)
> myKeys = myDictionary.keys()
> myKeys.sort()
> for key in myKeys:
> print myDictionary[key]

Fine!  Even faster & simpler:

    myItems = myDictionary.Items()
    myItems.sort()
    for key, value in myItems:
        print value


Alex






More information about the Python-list mailing list