Created
October 21, 2013 18:39
-
-
Save tkhn/7088731 to your computer and use it in GitHub Desktop.
Python: sorting dict by value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> 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