Newbie problem

Paul Simmonds psimmo60 at hotmail.com
Tue Dec 17 07:19:29 EST 2002


>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? :)
>
>// Teel

Assuming you're handling strings and rearranging the dictionary for 
printing, maybe you can swap the dictionary keys and values around then sort 
on keys? Like this:

def sortbyval(origdict):
    revdict={}
    for key in origdict:
        revdict[origdict[key]]=key
    keys=revdict.keys()
    keys.sort()
    for key in keys:
        print revdict[key],key

HTH,
Paul

**********************************************
I used to have a handle on life, but it broke.
**********************************************

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online 
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963





More information about the Python-list mailing list