Skip to content Skip to sidebar Skip to footer

What Does This Mean: Key=lambda X: X[1] ?

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, what is x:, why [1] in x[1] etc... Examples m

Solution 1:

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:

defelement_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.

Solution 2:

lambda signifies an anonymous function. In this case, this function takes the single argument x and returns x[1] (i.e. the item at index 1 in x).

Now, sort(mylist, key=lambda x: x[1]) sorts mylist based on the value of key as applied to each element of the list. Similarly, max(gs_clf.grid_scores_, key=lambda x: x[1]) returns the maximum value of gs_clf.grid_scores_ with respect to whatever is returned by key for each element.

I should also point out that this particular function is already included in one of the libraries: operator. Specifically, operator.itemgetter(1) is equivalent to your key.

Solution 3:

From a reference for Python 3.7 (https://docs.python.org/3/howto/sorting.html), The key is a parameter of list.sort() and sorted(). The first built-in function modifies a list in place while the latter accepts and return iterable.

The key parameter can be defined as a function to be called on each element of list/iterable before comparison and sort, respectively. In this case, the inline function lambda x: x[1] is defined as a value of the key parameter. The lambda function takes input x return x[1] which is the second element of x.

Supposed

mylist = [[7, 8], [1, 2, 3], [2, 5, 6]]
# list(map(lambda x: x[1], mylist)) returns [8, 2 ,5]mylistSort = sorted(mylist, key = lambda x: x[1])
# will sort the nested list based on the result of the lambda function 

Can you guess what the result? mylistSort is then [[1,2,3], [2,5,6], [7,8]] from the sorted sequence of [8,2,5] which is [2,5,8].

The max() in your example is applied to just get the max value from the outcome of the sort function.

I hope this post is helpful.

Solution 4:

One more example of usage sorted() function with key=lambda. Let's consider you have a list of tuples. In each tuple you have a brand, model and weight of the car and you want to sort this list of tuples by brand, model or weight. You can do it with lambda.

cars = [('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700)]

print(sorted(cars, key=lambda car: car[0]))
print(sorted(cars, key=lambda car: car[1]))
print(sorted(cars, key=lambda car: car[2]))

Results:

[('bmw', 'x5', 1700), ('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000)][('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700), ('citroen', 'xsara', 1100)][('citroen', 'xsara', 1100), ('bmw', 'x5', 1700), ('lincoln', 'navigator', 2000)]

Solution 5:

distances.sort(key=lambda x: x[1])

This is the function. And here x is the list, in which we are adding x[1] i.e 2nd element of list to the sort function. So, basically we are adding every list's 2nd element (i.e x[1]) to the sort function. I hope you understand this.

Post a Comment for "What Does This Mean: Key=lambda X: X[1] ?"