[Tutor] sorting by values in dict

Paul Tremblay phthenry at earthlink.net
Thu Nov 13 01:52:31 EST 2003


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,
}

I simply can't switch values and keys and sort by keys, because files
might have the same size. For example, if I simply switched values and
keys in the above dictionary, I would get:

{10000 : 'file1',
5000   : 'file3',
}

Obviously, I can sort the values like this:

values = the_dict.values()
values.sort()

But I need to know which value is associated with each key.

There is probably a very easy answer to this. My python book doesn't
mention it.

thanks

Paul


-- 

************************
*Paul Tremblay         *
*phthenry at earthlink.net*
************************



More information about the Tutor mailing list