I'm reading the python tutorial document and can't understand the result of one example in lambda function. the example is:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
pairs [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
In my understanding, pairs.sort(key=lambda pair: pair[1])
means sort the pairs using the result of lambda function. The argument of lambda function is pair
, and the result is pair[1]
, which means the second value of pair
list? Then how would this key get the sort of [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
? Could anyone nicely explain it for me?