pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
print(pairs)
Ans:
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
How does the pair argument work here?
When you want to sort a collection, the key parameter is a function that is used to extract from each element the value by which you want to sort. The function takes the argument, produces a value, and uses this value to sort the list
In your case, lambda pair: pair[1], is just an anonymous function that takes your (x, y) pairs of values and returns only the y. Since those values are strings in your case, your list is sorted in alphabetical order of the second value of each tuple.
keyargument tosortdoes? Have you read the documentation? What specifically are you asking about?key=operator.itemgetter(1)def something(pair): return pair[1]over each item in the list. It's not clear which part you're unsure about. The word "pair" itself has no specific meaning, call it whatever you like.