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
Write a Python program to sort a dictionary by key.
Write a Python program to combine two dictionaries adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400}
Sample output:
Counter({'a': 400, 'b': 400, 'c': 300, 'd': 400})
Write a Python program to sort a list alphabetically in a dictionary.
Write a Python program to count number of items in a dictionary value that is a list.