how to find the minimum value in a dictionary python

48

python get the key with the max or min value in a dictionary -

# Basic syntax:
key_with_max_value = max(dictionary, key=dictionary.get)

# Note, to get the max value itself, you can do either of the following:
max_value = dictionary[max(dictionary, key=dictionary.get)]
max_value = max(dictionary.values())

# Example usage:
dictionary = {"a": 1, "b": 2, "c": 3}
max(dictionary, key=dictionary.get)
--> 'c'

find keys to minimum value in dict -

# Works also with multiple equal minimum values
res = list(filter(lambda x: d[x]==minval, d))

Comments

Submit
0 Comments