0

I refer to the solution given for How to sort (list/tuple) of lists/tuples?.

sorted_by_second = sorted(data, key=lambda tup: tup[1])

or

data.sort(key=lambda tup: tup[1])  # sorts in place

Where is tup passed to the lambda function in either sort function?

chribonn
  • 445
  • 5
  • 20

4 Answers4

1

tup is the argument to lambda.

lambda tup: tup[1]

Is equivalent to:

def anonymous_func(tup):
    return tup[1]
rdas
  • 20,604
  • 6
  • 33
  • 46
1

By sort itself. The key parameter is a callback function which sort calls on each element of the sequence it is sorting. It lets you write something like the following in a more concise manner:

new_data = [(tup[1], tup) for tup in data]
tmp  = sorted(data)
sorted_by_second = [orig for _, orig in tmp]
chepner
  • 497,756
  • 71
  • 530
  • 681
1

In the lambda function lambda tup: tup[1], tup refers to the element of list data, and tup[1] refers to the second element of the item.

When you pass this to sorted function, you tell it to use the second element of the item in the list as a comparison key, so the result you get is the sorted list on the second element

From the docs

key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).

Hence the behaviour is as follows

In [54]: data = [[4,5,6], [1,2,3], [7,8,9]]                                                                                                                                                             

In [55]: sorted_by_second = sorted(data, key=lambda tup: tup[1]) 

In [56]: sorted_by_second                                                                                                                                                                               
Out[56]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Note that you can also use operator.itemgetter to do the same thing, which automatically picks the second element from the tuple

In [60]: import operator  
In [64]: data = [[4,5,6], [1,2,3], [7,8,9]]                                                                                                                                                             
In [65]: sorted_by_second = sorted(data, key=operator.itemgetter(1))                                                                                                                                    
In [66]: sorted_by_second                                                                                                                                                                               
Out[66]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

Here key is a transform function applied to each element of the array to provide a new value, which will then be used to sort the array. Essentially this makes it such that an element should be sorted before an element based if key(a) < key(b) rather than a < b.

The list.sort and sorted functions will call key for each element individually.

Quinn Mortimer
  • 671
  • 6
  • 14