How to get the key with maximum value in dictionary with Python?

Sometimes, we want to get the key with maximum value in dictionary with Python.

In this article, we’ll look at how to get the key with maximum value in dictionary with Python.

How to get the key with maximum value in dictionary with Python?

To get the key with maximum value in dictionary with Python, we can use the max method with the key set to the get method of the dictionary.

For instance, we write:

stats = {'a': 1000, 'b': 3000, 'c': 100}
max_stats = max(stats, key=stats.get)
print(max_stats)

to call max with the stats dictionary and key set to stats.get, which returns the value of the entry with the given key.

And then we assign the returned value to max_stats.

Therefore, max_stats is 'b'.

Conclusion

To get the key with maximum value in dictionary with Python, we can use the max method with the key set to the get method of the dictionary.