Force444
Force444

Reputation: 3381

Sort list after numerical value in string

I have a list, where each item has the format "title, runtime" e.g. "Bluebird, 4005"

How can I sort that list in Python, according to the runtime part of the string for each item?

Is there a way to do this without using regex?

Upvotes: 3

Views: 100

Answers (1)

thefourtheye
thefourtheye

Reputation: 239553

words_list = ["Bluebird, 4005", "ABCD, 1", "EFGH, 2677", "IJKL, 2"]
print sorted(words_list, key = lambda x: int(x.split(",")[1]))
# ['ABCD, 1', 'IJKL, 2', 'EFGH, 2677', 'Bluebird, 4005']

Upvotes: 10

Related Questions