Consider the following variable x = [[a, 2], [b, 1], [c, 0]]

using x.sort() will automatically sort alphabetically. how can one sort by the numeric numbers in x instead?

Consider the following variable x = [[a, 2], [b, 1], [c, 0]]

using x.sort() will automatically sort alphabetically. how can one sort by the numeric numbers in x instead?

Really simple problem, here is a quick link for it. It should help. If not then I apologize but I think it should hep you.

Links: How to Use sorted() and sort() in Python – Real Python

1 Like

You could use the key argument:

x = [['a', 2], ['b', 1], ['c', 0]]
def use_for_sort(val):
return val[1] # get 2nd thing in each element
x.sort(key = use_for_sort)
print(x)

1 Like

ah sweet, thanks for clarifying!