Reputation: 3381
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
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