Programming Problems · Q3
Q.Write a Python program to find the highest 2 values in a dictionary.
Rajasthan RbseTextbookSubjective· 3mImportance★★★★★est
70% · 21/30 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →sorted(d.values(), reverse=True) puts all values in descending order, so positions 0 and 1 are the two highest; sorting the keys with key=d.get names the students who hold them.
The idea. A dictionary gives us its values as a view via values(). To find the top two we do not need any manual comparison logic — the built-in sorted() with reverse=True arranges them largest-first, and the first two positions are the answer. As a bonus, sorting the keys by their value (key=marks.get) tells us which entries own those top values.
Program:
marks = {'Amit': 81, 'Bina': 92, 'Charu': 77, 'Divya': 88, 'Esha': 69}
values = sorted(marks.values(), reverse=True) # descending order
print("Highest value :", values[0])
print("Second highest value :", values[1])
top2 = sorted(marks, key=marks.get, reverse=True)[:2]
print("Students holding the highest 2 values:", top2)
Verified output:
Highest value : 92
Second highest value : 88
Students holding the highest 2 values: ['Bina', 'Divya']
Key lines:
sorted(marks.values(), reverse=True)->[92, 88, 81, 77, 69]; indexes[0]and[1]are the highest and second highest. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.