In this tutorial, we will look at how to get the key with the maximum value in a Python dictionary with the help of some examples.
How to get the key with the max value in a dictionary?

You can use the Python built-in max()
function to get the key with the maximum value in a dictionary. Pass the dictionary’s get function as an argument to the key
parameter. The following is the syntax:
# key with highest value in the dictionary max(sample_dict, key=sample_dict.get)
It returns the key which has the largest value in the dictionary.
Let’s look at an example.
We have a dictionary containing the salaries of the different employees in a company. You want to find the employee with the highest salary.
# create a dictionary salary = { "Michael": 90000, "Jim": 75000, "Dwight": 80000 } # get key with maximum value print(max(salary, key=salary.get))
Output:
Michael
Here we find the key in the dictionary salary
which has the maximum value. We find that “Michael” has the largest salary.
What would happen if there are more than one key with the maximum value? In that case, the max()
function would return the first key it encounters having the maximum value.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
To get all the keys with max value in a dictionary, you can use a list comprehension.
# create a dictionary salary = { "Michael": 90000, "Jim": 75000, "Dwight": 80000, "Ryan": 90000 } # get key with maximum value print([key for key in salary.keys() if salary[key]==max(salary.values())])
Output:
['Michael', 'Ryan']
We get all the employees with the maximum salary.
Using a loop
Alternatively, you can iterate through the dictionary items in a loop and keep track of the key with the maximum value.
Let’s use the same use-case as above. Finding the maximum salary in the dictionary of names to salaries.
# create a dictionary salary = { "Michael": 90000, "Jim": 75000, "Dwight": 80000 } # get key with maximum value max_val = None max_val_key = None for key, val in salary.items(): if max_val is None: max_val = val max_val_key = key if val > max_val: max_val = val max_key = key print(max_val_key)
Output:
Michael
We get the same result as above. “Michael” has the highest salary in the given dictionary. Using a loop may not be the best solution here. Other methods mentioned above are also straightforward and they require fewer lines of code.
You might also be interested in –
- Get Value for a Key in a Python Dictionary
- Count Occurrences of a Value in a Python Dictionary
- Check If a Python Dictionary Contains a Specific Key
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.