22

I've got a dict with string keys and int values. Is there any way I could take this dict and use it to get a list of the keys from highest to lowest value?

Example:

>>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8}
>>> myList = myDict.sortNumericallyByKeys
>>> myList
['eight', 'seven', 'five', 'four', 'two', 'one']
Lucas Phillips
  • 645
  • 1
  • 7
  • 14
  • @ᴋᴇʏsᴇʀ I looked thorough the data structures docs and didn't find anything. Where else could I look? – Lucas Phillips Nov 30 '13 at 19:45
  • @ᴋᴇʏsᴇʀ won't that sort the keys alphabetically, not by the values numerically? – Lucas Phillips Nov 30 '13 at 19:48
  • All I'm getting at is [this](http://meta.stackexchange.com/questions/182266/how-much-research-effort-is-expected-of-stack-overflow-users/182380#182380) :p – keyser Nov 30 '13 at 19:56
  • @GiovanniP [This](http://stackoverflow.com/q/4690094/645270) one's closer, but neither are exact duplicates. – keyser Nov 30 '13 at 20:04

4 Answers4

28
sorted(myDict, key=myDict.get, reverse=True)
iruvar
  • 22,736
  • 7
  • 53
  • 82
8

Here's one way of doing it:

>>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8} 
>>> sorted(myDict.iterkeys(), key=lambda k: myDict[k], reverse=True)
['eight', 'seven', 'five', 'four', 'two', 'one']

(Inspired by this answer)

It uses the built-in function sorted (with reverse=True to get highest to lowest), and the key argument is a function that sets the sort key. In this case it's a lambda that fetches the corresponding dict value, but it can be pretty much anything. For example, you could use operator.itemgetter(1) or myDict.get as shown by other answers, or any other sorting function (other than by value).

Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101
4

You can use items to get a list of pairs key-value and sorted to sort them using your criteria:

myList = sorted(myDict.items(), key=lambda x: x[1], reverse=True)

If you're using ipython, you can type myDict.tabtab and you're get a list of all functions. You can also type print myDict.items.__doc__ and get a quick documentation.

The key parameter is a function to apply to an element before it gets compared. As items returns a list of pairs and sorting is done on pairs' second element, key is a function which gets the second element from a tuple.

Of course, it is possible to get rid of the items call, using:

myList = sorted(myDict, key=myDict.get, reverse=True) #posted in another answer
kaspersky
  • 3,959
  • 4
  • 33
  • 50
2

Another variation:

import operator

d = {'q':2, 'x':1, 'b':10}
s = sorted(d.iteritems(), key=operator.itemgetter(1), reverse=True)

The operator module provides itemgetter which is designed to be used like this. It's faster than a lambda. ;-)

Edit:

I kinda misinterpreted the question. My solution returns a list of tuples (key, value) instead of just a list of strings.

As a bonus to make it up, have a look at collections.OrderedDict. You might also want to consider using the dict 'reversed'. e.g. switch the keys and values.

siebz0r
  • 18,867
  • 14
  • 64
  • 107
  • FYI, switching dictionary keys and values would be a little bizarre (and also more difficult than ordering the keys according the the values). If you wanted to do this, you probably picked the wrong data structure to begin with. – De Novo Aug 12 '17 at 21:17