Getting key with maximum value in dictionary?
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'
Getting the key for second maximum value from a dictionary, Python
Sort the dict keys by their value and get the second to last item:
sorted(dic, key=dic.get)[-2]
get the key of the maximum value in the dictionary among some values
Just pass dict.get
function as your key
parameter in max()
:
# to find the max of entire dictionary
max(D, key=D.get)
# 'W'
# to find individual keys
max(['N', 'S'], key=D.get)
# 'N'
Find ALL keys of highest value in dictionary
You can try this:
dictt = {'John': 10, 'Chase': 11, 'Cole': '11'}
max_val = max(dictt.values(), key = lambda x: int(x))
maxkeyarray = [key for key in dictt if int(dictt[key]) == max_val]
print(maxkeyarray)
Output:
['Chase', 'Cole']
print highest value in dict with key
You could use use max
and min
with dict.get
:
maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum.
print(maximum, mydict[maximum])
# D 87
get key of max value in dictionary when value is list
You can use the builtin max()
operator with a lambda
function
result = max(d.items(), key=lambda item: max(item[1]))
The result
of this statement is a tuple
('BATTERY', [0.5, 0.0])
Where you can simply take the first element - the key
see also this answer for the simpler case
Get keys with maximum value from a nested dictionary
In simple way with builtin features:
d = {'eyes_color': {"blue": 10, "brown": 12},
'hair_color': {"black": 15, "blonde": 7},
'gender': {"male": 16, "female": 6}
}
max_value, max_key = max(((v,k) for inner_d in d.values() for k,v in inner_d.items()))
print(max_key) # male
print(max_value) # 16
Python Get key of nested dictionary with highest value for attribute
You can find the maximum among the keys of your dictionary, comparing based on a custom key parameter that retrieves the amount corresponding to the key:
result = max(data.keys(), key=lambda x: data[x]['amount'])
print(result)
Get dict key by max value
Use the key
parameter to max()
:
max(d, key=d.get)
Demo:
>>> d= {'a':2,'b':5,'c':3}
>>> max(d, key=d.get)
'b'
The key
parameter takes a function, and for each entry in the iterable, it'll find the one for which the key
function returns the highest value.
get key of max value in dictionary when value is list
max(myDict, key=lambda x: max(myDict[x]))
Related Topics
Why Is Reading Lines from Stdin Much Slower in C++ Than Python
Decode HTML Entities in Python String
How to Select a HTML Element No Matter What Frame It Is in in Selenium
Render HTML to Pdf in Django Site
Checking If Element Exists With Python Selenium
How to Get Text of an Element in Selenium Webdriver, Without Including Child Element Text
Beautiful Soup Findall Doesn't Find Them All
Staleelementreferenceexception on Python Selenium
Convert Rgb Color to English Color Name, Like 'Green' With Python
How to Get Keyboard Input in Pygame
Tkinter Assign Button Command in a For Loop With Lambda
Limiting Floats to Two Decimal Points