1

Say if you have a list like this:

List = [["james",3],["harry",1],["Joe",2]]

How would you sort it so that it sorts the list by the numbers instead of the names? i want it to produce this:

List = [["harry",1],["Joe",2],["james",3]]

What i'm currently doing:

List.sort(key=List[1])

Not sure why this doesnt work though? The website im using to learn this isnt very clear at all really.

snaktastic
  • 43
  • 1
  • 1
  • 6

1 Answers1

1

Try:

List.sort(key=lambda x: x[1])

x here refers to an element in List

sam46
  • 1,273
  • 9
  • 12