Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

key corresponding to maximum value in python dictionary

Tags:

python

a = dict(((1,3),(0,-1),(3,21)))
m = max(a, key=a.get)

Can someone give me an explanation on why this returns the key corresponding to the maximum value? It's stumped me for quite a while. Thanks!

like image 202
Glassjawed Avatar asked Mar 08 '11 01:03

Glassjawed


People also ask

How do you find the maximum value of a dictionary key?

Use max() and dict. get to find the key with the max value in a dictionary. Call max(iterable, key=dict. get) with the same dictionary as both iterable and dict to find the key paired with the max value.

How do I return a key to the highest value in Python?

Python find highest value in dictionary By using the built-in max() method. It is provided with the 'alpha_dict' variable to obtain the highest value from and to return the key from the given dictionary with the highest value, the dict. get() method is used.

What is key in max function Python?

key (optional) It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable. If max() is called with an iterable, it returns the largest item in it. If the iterable is empty then the default value is returned, otherwise, a ValueError exception is raised.

How do you get the key of the minimum value in a dictionary?

Use the zip function to create an iterator of tuples containing values and keys. Then wrap it with a min function which takes the minimum based on the first key. This returns a tuple containing (value, key) pair. The index of [1] is used to get the corresponding key.


2 Answers

The dictionary "a" is an iterable structure in python. When you loop through with for x in a, you are looping over the keys in the dictionary.

In the second line, the max function takes two arguments: An iterable object (a), and an optional "key" function. The Key function is going to be used to evaluate the value of the items in a--the largest of which will be returned.

Examples:

>>> a = dict(((1,3),(0,-1),(3,21)))
>>> for x in a:
...     print x #output the value of each item in our iteration
... 
0
1
3

Note here that only the "keys" are output. When we pass each of these keys to "get"...

>>> a.get(0)
-1
>>> a.get(1)
3
>>> a.get(3)
21

We get the value for each key. Now see how max works.

>>> b=[2, 3, 5, 6, 4]
>>> max(b)
6
>>> def inverse(x):
...     return 1.0 / x
... 
>>> max(b, key=inverse)
2

As you can see, max (by default) will just find the largest number in our iterable list. If we define the "inverse" function, it will return the largest item in b for which inverse(item) is the largest.

Combine these two items and we see that max(a, key=a.get) is going to return the item in a for which the value of a.get(item) is largest. i.e. the key corresponding to the largest value.

like image 83
ProdigySim Avatar answered Oct 06 '22 04:10

ProdigySim


Are you asking how the key parameter works? It takes a callable, which is applied to every element in the dictionary. In the case of your dictionary, it simply does this:

a.get(1)  # 3
a.get(0)  # -1
a.get(3)  # 21

max then uses the result of the key function as the comparison value, so it will obviously choose the element which returns the highest value, ie 3.

like image 43
Daniel Roseman Avatar answered Oct 06 '22 05:10

Daniel Roseman