[Tutor] sorting by values in dict

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Nov 13 13:09:34 EST 2003



On Thu, 13 Nov 2003, Paul Tremblay wrote:

> Is there a way to sort a dictionary by values?
>
> My problem involves a simple script that sorts files by size.
>
> I make dictionary that looks like this:
>
> {'file1': 10000,
> 'file2'	: 10000,
> file3'	: 5000,
> }
>


Hi Paul,


You can probably take the key-value pairs by using the items() method:

###
>>> d = {'file1': 10000, 'file2' : '10000', 'file3' : 5000}
>>> d.items()
[('file3', 5000), ('file2', '10000'), ('file1', 10000)]
###


Hope this helps!




More information about the Tutor mailing list