Jan-25-2022, 06:23 PM
(This post was last modified: Jan-25-2022, 08:53 PM by deanhystad.)
x = ['Finley : 10', 'Evie : 0', 'P1 : 0', 'P2 : 5', 'P1 : 0', 'P2 : 5', 'Finley : 15', 'Evie : 5'] x.sort(key=lambda x: int(x.split(":")[1])) print(x)
Output:['Evie : 0', 'P1 : 0', 'P1 : 0', 'P2 : 5', 'P2 : 5', 'Evie : 5', 'Finley : 10', 'Finley : 15']
Could also use the name for when the score is the same.x = ['Finley : 10', 'Evie : 0', 'P1 : 0', 'P2 : 5', 'P1 : 0', 'P2 : 5', 'Finley : 15', 'Evie : 5'] x.sort(key=lambda x: (int(x.split(":")[1]), x)) # Sort key is a tuple(number, name) print(x)
Output:['Evie : 0', 'P1 : 0', 'P1 : 0', 'Evie : 5', 'P2 : 5', 'P2 : 5', 'Finley : 10', 'Finley : 15']