KnowledgeBoat Logo

Computer Science

Write a Python program to find the three highest values in a dictionary.

Python

Python Dictionaries

3 Likes

Answer

my_dict = eval(input("Enter the dictionary: "))
highest_values = []
highest_keys = []
for key, value in my_dict.items():
    if not highest_values or value > highest_values[-1]:
        highest_values.append(value)
        highest_keys.append(key)
        if len(highest_values) > 3:
            highest_values.pop(0)
            highest_keys.pop(0)
print("Three highest values in the dictionary:")
for i in range(len(highest_keys)):
    print(highest_keys[i], ":" ,highest_values[i])

Output

Enter the dictionary: {'c': 100, 'b': 200, 'e': 300, 'd': 400, 'a': 500} 
Three highest values in the dictionary:
e : 300
d : 400
a : 500

Answered By

1 Like


Related Questions