1
stocks ={
    'BMW':40 ,
    'FB': 90 ,
    'whatsapp':100,
    'Apple':30
    }
print("lowest stock is : " , min(stocks ,key =lambda x:stocks[x]) )

this code works but i need to know how lambdas work in this context i know that it take an argument then return the result which is the expression ,so my question is what the argument which in this example lambda takes 'x' ,and does this argument change also or not .I mean does min function change it when it runs.so when min is called for first time what is the value of x.thanks in advance.

khaled
  • 69
  • 1
  • 7
  • 2
    lambda isn't the one doing any magic here. Lambda is just a function without a name. for every item x passed to it, it tries to access `stocks[x]` for that x. The main thing to note is that `min` is accepting a function as `key` on which to evaluate which value is smallest. – Paritosh Singh Feb 27 '19 at 14:37
  • Possible duplicate of [Get the key corresponding to the minimum value within a dictionary](https://stackoverflow.com/questions/3282823/get-the-key-corresponding-to-the-minimum-value-within-a-dictionary) – Welbog Feb 27 '19 at 14:40
  • 1
    Unrelated, but you could also just do `key=stocks.get` – tobias_k Feb 27 '19 at 14:45
  • What part of the [documentation](https://docs.python.org/3/library/functions.html#max) of the `max()` function don't you understand? In your code, the `x` is will be one of the items in the `stocks` dictionary. – martineau Feb 27 '19 at 14:47

2 Answers2

3

From https://www.programiz.com/python-programming/methods/built-in/min:

  1. min(iterable, *iterables[, key, default])
    • iterable - sequence (tuple, string), collection (set, dictionary) or an iterator object whose smallest element is to be found
    • *iterables (Optional) - any number of iterables whose smallest is to be found
    • key (Optional) - key function where the iterables are passed and comparison is performed based on its return value
    • default (Optional) - default value if the given iterable is empty

The call min(stocks, key= lambda x:stocks[x]) is finding the smallest element in stocks, by comparing the results of the function lambda x:stocks[x], which is just an anonymous function that returns the value of the stock for a given key. So the call to min returns the stock with the smallest value.

See also Get the key corresponding to the minimum value within a dictionary and http://www.aroseartist.com/python-dictionary-max-min/

Welbog
  • 59,154
  • 9
  • 110
  • 123
0

Your min() function gets an iterable as first argument (in this case 'stocks' dict).

The second argument, which is 'key = lambda x: stocks[x]', is a function that specifies based on what minimum should be evaluated.

Basically x takes the following values ('BMW' 'FB' 'whatsapp' 'Apple') and lambda function returns the values (40 90 100 30). 30 is the lowest value and thus your min() function will return the 'key' for that value, which is 'Apple' (not to be confused with the 'key', which is an argument in min() function)

Tatevik
  • 43
  • 4