How to Get All Keys with the Highest Value in Python

Published Sep 13, 2021

What is a simple way to obtain all the keys with the highest value in a Python dictionary?

frequency = {
    'a': 1,
    'b': 999999,
    'c': 56,
    'd': 999999

In this problem, we’re assuming there can be multiple keys with the same maximum value. We want to return a list of those keys.

['b', 'd']

We can first get the maximum value of all keys in our dictionary.

Then, we can loop through our dictionary to get all keys with that value.

max_value = max(frequency.values())
res = [k for k,v in frequency.items() if v == max_value]

More Python Articles