how to sort dict by value

Solutions on MaxInterview for how to sort dict by value by the best coders in the world

showing results for - "how to sort dict by value"
Ugo
23 Jul 2018
1dict1 = {1: 1, 2: 9, 3: 4}
2sorted_dict = {}
3sorted_keys = sorted(dict1, key=dict1.get)  # [1, 3, 2]
4
5for w in sorted_keys:
6    sorted_dict[w] = dict1[w]
7
8print(sorted_dict) # {1: 1, 3: 4, 2: 9}
9