[Tutor] sorting by values in dict

Gabriel Cooper gabriel.cooper at mediapulse.com
Thu Nov 13 13:09:08 EST 2003



Paul Tremblay wrote:

>Is there a way to sort a dictionary by values?
>
You can do this:

 >>> x = { 'file1':10000, 'file2':10000, 'file3':5000 }
 >>> y = x.keys()
 >>> y.sort()
 >>> for key in y:
    print str(key)+", "+ str(x[key])

   
file1, 10000
file2, 10000
file3, 5000


It's not pretty, but it works.




More information about the Tutor mailing list