I am studying the Python Cookbook, 3rd edition. I encountered the code below:
prices = {'ACME': 45.23,'AAPL': 612.78,'IBM': 205.55,'HPQ': 37.20,'FB': 10.75}
min(prices, key=lambda k: prices[k])
I tested this on Python 3.6.0, and it returns 'FB'.
My understanding is that every key from the prices dictionary is sent to the lambda as parameter k and then the lambda returns the value associated with key k in the prices dictionary. So after iterating through all the keys, a list of values is returned which is used as an argument to the min function which finally returns the minimum value (in this case 10.75) in the dictionary.
But when I ran the code, the program printed out the key (in this case 'FB'). Where am I wrong?
I have gone through the documentation. It said min returns the smallest item in an iterable. I know min processes the keys and not the values. I just don't understand how it returns the key when the lambda returns a list of values. Help me please.
EDIT: Sorry for the edit. I am trying to find the key of the smallest value in the dictionary. The above code works perfectly but I don't know how it works.