In this python dictionary program, we will find the keys of maximum values in a given dictionary.
Steps to solve the program
- Take a dictionary as input.
- Sort the given dictionary in descending order by using sorted().
- In sorted use dictionary values as key to sort the dictionary using get().
- Print the keys of the first 2 maximum values in the dictionary using indexing.
D1 = {'a':18,'b':50,'c':36,'d':47,'e':60}
result = sorted(D1, key=D1.get, reverse=True)
print(result[:2])
Output :
['e', 'b']