Created
February 11, 2021 16:57
-
-
Save AColocho/1cac537de07ccdf2e029a0c76ab79047 to your computer and use it in GitHub Desktop.
One liner to order a dictionary by value or key.
This file contains 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
#source https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value | |
#Replace x with the dictionary name | |
#If ordering by value item[1] else item[0] | |
{k: v for k, v in sorted(x.items(), key=lambda item: item[1])} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment