Programming Problems · Q8
Q.Write a Python program to find the highest 2 values in a dictionary.
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
93% · 28/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 →Take all the values with d.values(), sort them in descending order, and slice the first
two: sorted(d.values(), reverse=True)[:2]. A variant with sorted(d, key=d.get, ...) also
recovers which keys own those top values.
The idea
The dictionary's values form a plain collection once you call .values(), so "highest 2 values"
reduces to a sort-and-slice on that collection.
Program
# Highest 2 values in a dictionary
marks = {"Amit": 78, "Bina": 92, "Chirag": 85, "Deepa": 67, "Esha": 90}
top_two = sorted(marks.values(), reverse=True)[:2]
print("Highest 2 values:", top_two)
Highest 2 values: [92, 90]
Also want to know WHO scored them?
Sort the keys by their value (key=marks.get tells sorted() to compare each key's value):
top_keys = sorted(marks, key=marks.get, reverse=True)[:2]
for k in top_keys:
print(k, "->", marks[k])
Bina -> 92
Esha -> 90
Key lines explained: …
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.