I want to sort each string in a list of string to be in alphabetical order.
For example,
l1 = ["bba", "yxx", "abc"]
I want to sort it to be
l1 = ["abb", "xxy", "abc"]
I can do it in a for loop but wonder if this is a more pythonic way using python list comprehension. Thanks :D
Using list comprehension and str.join:
>>> l1 = ["bba", "yxx", "abc"]
>>> [''.join(sorted(s)) for s in l1]
['abb', 'xxy', 'abc']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With