Keyword Functions for Python min / max


I am trying to understand how this works:

my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)



Which is a really cool feature and one I want to understand better.
Based on the documentation

min(iterable[, key]) Return the smallest item in an iterable or the smallest of two or more arguments... The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).

Where can I find out more about available functions? In the case of a dictionary, is it all the dictionary methods?

Edit: I came across this today:

max(enumerate(array_x), key=operator.itemgetter(1))

Still looking for information on available keyword functions for min/max

The code you have written is

my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)

actually this works on min function. so, what does min do?

min(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its lowest item. With two or more arguments, return the lowest argument.

The key here is used to pass a custom comparison function.

Example: output max by length of list, where arg1, arg2 are both lists.

>>>> max([1,2,3,4], [3,4,5], key=len)
[1, 2, 3, 4]

But what if I want the max from the list, but by considering the second element of the tuple? here we can use functions, as given in official documentation. The def statements are compound statements they can't be used where an expression is required, that's why sometimes lambda's are used.

Note that lambda is equivalent to what you'd put in a return statement of a def. Thus, you can't use statements inside a lambda, only expressions are allowed.

>>> max(l, key = lambda i : i[1])
(1, 9)

# Or

>>> import operator
>>> max(l, key = operator.itemgetter(1))
(1, 9)

so the functions are basically depend upon the the iterable and and passing the criteria for the comparison.

Now in your example, you are iterating over your dictionary. And in key, you are using get method here.

The method get() returns a value for the given key. If key is not available then returns default value None.

As here, no arguments are there in get method it simply iterates over values of dictionary. And thus the min gives you the key having minimum value.

For max(enumerate(array_x), key=operator.itemgetter(1)) we want to compare the values of array instead of their indices. So we have enumerated the array.

enumerate(thing), where thing is either an iterator or a sequence, returns a iterator that will return (0, thing[0]), (1, thing1), (2, thing[2])

now we have used itemgetter function of operator module. operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.

you can also use lambda function of here like

max(enumerate(array_x), key=lambda i: i[1])

So the range of functions in key is almost up to the use. we can use many functions but the sole motive is , it is the criteria for that comparison.