🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-key-with-maximum-value-in-dictionary
Get key with maximum value in Dictionary - Python
In this method we will use the built-in max() function to identify the key with the highest value in the dictionary and by specifying key=d.get we can ensure that the comparison is based on the dictionary values rather than the keys.
Published   July 11, 2025
Discussions

Dictionary-maximum value
If a have a dictionary with keys and the value for each key, for example: Anne=3, Mark=5, Eve=2 How can I create a loop that gives the key with the highest value and then print both of them (the key, and the value, in the example above it would be Mark 5)? Thank you in advance More on discuss.python.org
🌐 discuss.python.org
0
0
December 13, 2021
python - Return the maximum value from a dictionary - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Closed 8 years ago. I want to return the maximum value and its key from a dictionary, and I know that something like the following should do the trick More on stackoverflow.com
🌐 stackoverflow.com
Confused about using max() to find the max value of a dict. See code example
key is a keyword argument for max which specifies how the max value is determined. In this case the code prints the key in the dictionary which holds the highest value. In other words, max iterates over dict (not a good idea to use built-in names btw), getting the keys, and then it keeps track of the values provided by dict.get(key) to determine the key with the highest value. Since an example is worth a thousand words, here's an old Gist I wrote years ago that effectively implements max. More on reddit.com
🌐 r/learnpython
7
1
November 2, 2023
Max in dict not returning highest value. How is Python determining the highest value in this code?
To sum up: max_key = max(dict) # dict values are ignored max_value = max(dict.values()) # doesn't work if there's both int and str, can't be compared for order max_item = max(dict.items()) # first compared by key, and if keys are same, compared by value. Also error arises if str vs int have to be compared. key_for_max_value = max(dict.items(), key=lamba item: item[1])[0] # return the key for the max value. Again, doesn't work if there's values that can't be compared for order, like int and str against each other More on reddit.com
🌐 r/learnpython
31
37
June 25, 2022
🌐
Reddit
reddit.com › r/learnpython › find a maximum value in dictionary.
r/learnpython on Reddit: Find a maximum value in dictionary.
June 21, 2021 -

I have a dictionary that has years as the key and the number of people born in that year as the value. I need to find the year with maximum births, and if there are multiple such years, return the smallest one. i.e if 1993 and 2007 both had 50 births (while the rest had lower than 50), the answer would be 1993.

So my naïve approach was :

year,pop=2051,float("-inf")
        for i in dic.keys():
            if dic[i]>pop:
                pop=dic[i]
                year=i
            elif dic[i]==pop:
                year=min(i,year) 

I was wondering if there was a more efficient way to do this. Possibly using the max() function combined with a lambda?

Top answer
1 of 5
4
Yes - you could use .items() to get an iterable of tuples (key, value), then apply max with key set to a lambda returning a tuple of the value (to get the maximum number) and the negative key (to get the least year out of the possibilities).
2 of 5
2
TL;DR: # d is the dictionary with key years and value number of births max(d.items(), key=lambda x: (x[1], -x[0])) max, min and sorted/sort have a keyword argument called key which you can use to sort using any function (so you can sort a list by mapping it to another list as well). The key can also return multiple values, so you can sort first by something, then by something else (see the example below, I'm not sure I can explain this well enough). If you have a dictionary d = {1993: 50, 1994: 40, ..., 2007: 50, 2008: 23, ...} and you want to get the year with the largest number of births, you can do this: y_max = max(d, key=d.get) which will give you the year (iterating over a dictionary == iterating over its keys in order of insertion (this last bit is Python 3.6+)). If you want both year and number of births, you can use d.items(). This will give you a sequence of (key, value) tuples. So something like [(1993, 50), (1994 ,40), ... (2007, 50), (2008, 23)]. The elements of the sequence are tuples and if you want the maximum by the second value (year), you can do something like this: y_max, num_births_max = max(d.items(), key=lambda x: x[1]) If there are multiple values having the same maximum, max returns the first one that comes in the sequence. If your dictionary is already sorted by year, you are done. However, it is always better to be explicit about the sorting that you want. So you can have the lambda function return 2 values y_max, num_births_max = max(d.items(), key=lambda x: (x[1], -x[0])) Since we asked Python to sort by -year after sorting by num_births, it will give the smallest year that has the most number of births. If you gave (x[1], x[0]) instead, it would give the largest year that had the most number of births.
🌐
Python.org
discuss.python.org › python help
Dictionary-maximum value - Python Help - Discussions on Python.org
December 13, 2021 - If a have a dictionary with keys and the value for each key, for example: Anne=3, Mark=5, Eve=2 How can I create a loop that gives the key with the highest value and then print both of them (the key, and the value, in…
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Maximum/Minimum Values and Keys in Python Dictionaries | note.nkmk.me
May 14, 2023 - Get value from dictionary by key with get() in Python · Since dictionaries iterate through their keys, applying the get() method to these keys retrieves their corresponding values. These values are then used to determine the maximum and minimum.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python max value in dictionary
Python Max Value in Dictionary - Spark By {Examples ...
May 31, 2024 - It iterates over each key in the dictionary, retrieves the maximum value from the corresponding list using the inner max() function, and then takes the overall maximum value from all the lists using the outer max() function.
🌐
PythonHello
pythonhello.com › problems › dict › get-key-with-max-value-in-dict
Getting the Key with the Maximum Value in a Dictionary in ...
To get the key with the maximum value in a dictionary, we can pass the dictionary to the max() function as an iterable and use the key parameter to specify the key function, which returns the value to be used for the comparison.
Find elsewhere
🌐
datagy
datagy.io › home › python posts › python: get dictionary key with the max value (4 ways)
Python: Get Dictionary Key with the Max Value (4 Ways) • datagy
February 23, 2022 - The simplest way to get the max value of a Python dictionary is to use the max() function. The function allows us to get the maximum value of any iterable. Let’s see how it works with a list, before diving into a more complex example with ...
🌐
AskPython
askpython.com › home › getting key with maximum value in the dictionary
Getting Key with Maximum Value in the Dictionary - AskPython
February 23, 2023 - We use the max() function to find the maximum value in the dictionary. Inside the max function, we used the zip and lambda functions which helped us to find the keys with the maximum value.
🌐
GeeksforGeeks
geeksforgeeks.org › python-get-key-with-maximum-value-in-dictionary
Get key with maximum value in Dictionary - GeeksforGeeks
The goal is to identify the smallest value in the dictionary and then collect every key that matches it. For example, in {'a': 3, 'b': 1, 'c': 2, 'd': 1}, the minimum value is 1, so the res · 4 min read Python - Key with Maximum element at Kth index in Dictionary Value List
Published   May 6, 2025
🌐
Python Guides
pythonguides.com › python-find-max-value-in-a-dictionary
How To Find Max Value In Python Dictionary
November 4, 2025 - The key=sales_data.get part tells Python to compare the dictionary values, not the keys. I often use this approach when I need to quickly identify the top-performing category or region in a dataset. Sometimes, I prefer to use a manual approach, especially when teaching beginners or debugging code. This method uses a simple for loop to iterate through the dictionary and keep track of the maximum value and its corresponding key.
🌐
Quora
quora.com › How-do-you-find-the-maximum-value-of-a-dictionary-in-Python
How to find the maximum value of a dictionary in Python - Quora
To find the maximum value in a Python dictionary you need to decide whether “maximum” means the largest value, the key associated with the largest value, or both. Below are concise, idiomatic patterns for each using Python 3.x. ... Raises ...
🌐
YouTube
youtube.com › finxter - create your coding business
How to get the key with the maximum value in a dictionary? - YouTube
I have spent my morning hours with an important mission: finding the cleanest, fastest, and most concise answer to this question. I realized that many answer...
Published   August 21, 2019
Views   2K
🌐
Delft Stack
delftstack.com › home › howto › python › find max value in dictionary python
How to Find Maximum Value in Python Dictionary | Delft Stack
February 2, 2024 - In Python 3.x, you can use the dict.items() method to iterate over key-value pairs of the dictionary. It is the same method as dict.iteritems() in Python 2. ... import operator stats = {"key1": 20, "key2": 35, "key3": 44} max_key = ...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to get the key with maximum value in a python dictionary?
How to Get the Key with Maximum Value in a Python Dictionary? - Be on the Right Side of Change
April 15, 2022 - For example, Python uses iterators in for loops to go over all elements of a list, all characters of a string, or all keys in a dictionary. When you specify the key argument, define a function that returns a value for each element of the iterable. Then each element is compared based on the return value of this function, not the iterable element (the default behavior). ... lst = [2, 4, 8, 16] def inverse(val): return -val print(max(lst)) # 16 print(max(lst, key=inverse)) # 2
🌐
TutorialsPoint
tutorialspoint.com › get-key-with-maximum-value-in-dictionary-in-python
Get key with maximum value in Dictionary in Python
import operator dictA = {"Mon": 3, "Tue": 11, "Wed": 8} print("Given Dictionary:\n",dictA) # Using max and get MaxKey = max(dictA.items(), key = operator.itemgetter(1))[0] print("The Key with max value:\n",MaxKey)
🌐
Deanagan
deanagan.github.io › Learning-Python-Getting-Key-Of-Max-Value-In-Dictionary
Learning Python Getting Key Of Max Value In Dictionary - Pixels, Patterns and Patience
January 18, 2021 - In our case, we want to get the name of the player who has the “max” average score. Python’s max function accepts a key which can be used as criteria for getting the max value. Let’s examine the max function. ... The dictionary input will be our iterable.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to find the maximum value in a python dict?
How to Find the Maximum Value in a Python Dict? - Be on the Right Side of Change
January 12, 2023 - Pass a key lambda function into max() returning the second tuple value to be the basis of comparison · # mg Omega 3 per 100g d = { "Salmon" : 2260, "Hering" : 1729, "Sardines" : 1480, "Flaxseeds" : 53400, "Eggs" : 400 } max_val = max(d.items(), key=lambda x: x[1]) print(max_val) ... Pass the dictionary into it—per default, it finds the maximum key. Set the optional key function to d.get that uses the value associated to the key as a basis for comparison.