-4

I want to sort this list by numbers, but I do not know how. I want to sort parenthesses by numbers, but if i do list.sort, it sorts by keys.

list = [("Max", 25), ("Lisa", 10), ("Jacob", 34)]
list.sort()
print(list)

how can i do it? Thanks.

AKX
  • 152,115
  • 15
  • 115
  • 172
Jadam
  • 23
  • 3

1 Answers1

-1

The .sort() method (and the sorted() function) accept a key= parameter, the value of which is a function to use for getting the sorting criterion for a given item.

If you want to sort the tuples (not "parentheses") by the second item, pass a function that returns the second item.

list.sort(key=lambda item: item[1])
AKX
  • 152,115
  • 15
  • 115
  • 172