From the Python documentation regarding using lambdas with the sort
method:
>>> 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')]
So looking for a sanity check to confirm I’m understanding this correctly: The lambda in this example takes in a pair (in this case, a tuple,) and sorts on the 2nd element (or the 2nd value; I’m not sure of nomenclature) of the tuple.
Since the 2nd element is a string, the sort surfaces ‘alphabetically inferior’ values to the top. Thus, the output has tuples with alphabetically inferior 2nd elements at the top of the collection.
Is my understanding correct?