Python Practice › Dictionary Programs › Program 46
Sort items in descending order of value
Write a Python program to sort a dictionary’s items by value, highest first.
Input
{"Math": 70, "Physics": 90, "Chemistry": 67}Output
{'Physics': 90, 'Math': 70, 'Chemistry': 67}Approach
sorted(..., key=..., reverse=True).
Solution
d = {"Math": 70, "Physics": 90, "Chemistry": 67}print(dict(sorted(d.items(), key=lambda kv: kv[1], reverse=True)))Output
{'Physics': 90, 'Math': 70, 'Chemistry': 67}Learn the concept
New to this topic? Read the lesson, then come back.