Keys of the top 2 values

EasyDictionary Programs · 72 of 94

Write a Python program to find the keys of the two largest values.

Advertisement
Input
{"a": 18, "b": 50, "c": 36, "d": 47, "e": 60}
Output
['e', 'b']

Approach

  1. Sort keys by value, descending, and take two.

Solution

Python
d = {"a": 18, "b": 50, "c": 36, "d": 47, "e": 60}print(sorted(d, key=d.get, reverse=True)[:2])
Output
['e', 'b']

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs