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.
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])