Home > database >  sort nested list by int
sort nested list by int

Time:12-10

how do I sort the following nested list, I want to sort it by number:

highscorelist = [["erik", 2],["oskar", 5], ["markus", 3],["peter", 1]]

I want the list it to look like this:

sorted_highscorelist= [["peter", 1],["erik", 2],["markus", 3],["oskar", 5]]

CodePudding user response:

You can use a lambda function for this:

sorted(highscorelist, key = lambda x: x[1])

CodePudding user response:

You can sort it using the sorted method, and a lambda function.

Example:

highscorelist = [["erik", 2],["oskar", 5], ["markus", 3],["peter", 1]]
sorted_highscorelist = sorted(highscorelist, key=lambda x: x[1])
  • Related