I see it used in sorting, but what do the individual components of this line of code actually mean?
key=lambda x: x[1]
What\'s lambda
lambda
effectively creates an inline function. For example, you can rewrite this example:
max(gs_clf.grid_scores_, key=lambda x: x[1])
Using a named function:
def element_1(x):
return x[1]
max(gs_clf.grid_scores_, key=element_1)
In this case, max()
will return the element in that array whose second element (x[1]
) is larger than all of the other elements' second elements. Another way of phrasing it is as the function call implies: return the max element, using x[1]
as the key.