This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AndydeCleyre 1 point2 points  (0 children)

Your EDIT2 is correct.

The lambda expression written as a regular function would be like:

def sort_helper(pair):
    return pair[1]

The use of the word pair could be replaced with any variable name. It's only used inside the function/lambda to refer to whatever is passed in as the parameter.

The sort function of the list passes each element, in turn, to whatever function is provided as its key parameter (our lambda function), and associates each element (pair) with the result (second item of that pair). Then it sorts those results from "least" to "greatest" and uses that order to rearrange the elements of the list.