max(stats, key=stats.get)
Answer from A. Coady on Stack Overflowmax(stats, key=stats.get)
You can use operator.itemgetter for that:
import operator
stats = {'a': 1000, 'b': 3000, 'c': 100}
max(stats.iteritems(), key=operator.itemgetter(1))[0]
And instead of building a new list in memory use stats.iteritems(). The key parameter to the max() function is a function that computes a key that is used to determine how to rank items.
Please note that if you were to have another key-value pair 'd': 3000 that this method will only return one of the two even though they both have the maximum value.
>>> import operator
>>> stats = {'a': 1000, 'b': 3000, 'c': 100, 'd': 3000}
>>> max(stats.iteritems(), key=operator.itemgetter(1))[0]
'b'
If using Python3:
>>> max(stats.items(), key=operator.itemgetter(1))[0]
'b'
Dictionary-maximum value
python - Return the maximum value from a dictionary - Stack Overflow
Confused about using max() to find the max value of a dict. See code example
Max in dict not returning highest value. How is Python determining the highest value in this code?
Videos
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?
The solution using list comprehension:
dic={0: 1.4984074067880424, 1: 1.0984074067880423, 2: 1.8984074067880425, 3: 2.2984074067880425, 4: 2.2984074067880425}
max_value = max(dic.values()) # maximum value
max_keys = [k for k, v in dic.items() if v == max_value] # getting all keys containing the `maximum`
print(max_value, max_keys)
The output:
2.2984074067880425 [3, 4]
You can first determine the maximum with:
maximum = max(dic.values())
and then filter based on the maximum value:
result = filter(lambda x:x[1] == maximum,dic.items())
Example in the command line:
$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dic={0: 1.4984074067880424, 1: 1.0984074067880423, 2: 1.8984074067880425, 3: 2.2984074067880425, 4: 2.2984074067880425}
>>> maximum=max(dic.values())
>>> maximum
2.2984074067880425
>>> result = filter(lambda x:x[1] == maximum,dic.items())
>>> result
[(3, 2.2984074067880425), (4, 2.2984074067880425)]
Given you want to present the list of keys are a nice list and the value, you can define a function:
def maximum_keys(dic):
maximum = max(dic.values())
keys = filter(lambda x:dic[x] == maximum,dic.keys())
return keys,maximum
which returns a tuple containing the list of keys and the maximum value:
>>> maximum_keys(dic)
([3, 4], 2.2984074067880425)
dict={}
print(max(dict, key=dict.get)So first, why do you need the key=dict.get part? Doesn't dict.get get the value of a key? So why would you assign key=value? Shouldn't it be value=dict.get? And then you get the max value of the values?