Skip to content

Instantly share code, notes, and snippets.

@tkhn
Created October 21, 2013 18:39
Show Gist options
  • Select an option

  • Save tkhn/7088731 to your computer and use it in GitHub Desktop.

Select an option

Save tkhn/7088731 to your computer and use it in GitHub Desktop.
Python: sorting dict by value
>>> d = {'Canada': 513, 'Sao Tome and Principe': 3, 'Fiji': 1, 'Montenegro': 12, 'Lithuania': 47}
>>> sorted_list = sorted(d, key=d.get, reverse=True)
>>> for i in sorted_list:
... print i, d[i]
...
Canada 513
Lithuania 47
Montenegro 12
Sao Tome and Principe 3
Fiji 1
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment