Python Practice › Dictionary Programs › Program 72
Keys of the top 2 values
Write a Python program to find the keys of the two largest values.
Input
{"a": 18, "b": 50, "c": 36, "d": 47, "e": 60}Output
['e', 'b']
Approach
- Sort keys by value, descending, and take two.
Solution
d = {"a": 18, "b": 50, "c": 36, "d": 47, "e": 60}print(sorted(d, key=d.get, reverse=True)[:2])Output
['e', 'b']
Learn the concept
New to this topic? Read the lesson, then come back.