CBSE Class 12 Computer Science
Question 49 of 57
Review of Python Basics — Question 49
Back to all questions 49
Question Write a Python program to find the three highest values in a dictionary.
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])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
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