Sort a string in the list alphabetacaly


i have the following list:

strs = ["tea","tea","tan","ate","nat","bat"]

i want to sort the strings in that list to be like that:

strs = ["aet","aet","ant","aet","ant","abt"]

how i can do this ?


>Solution :

Try like below:

>>> [''.join(sorted(s)) for s in strs]
['aet', 'aet', 'ant', 'aet', 'ant', 'abt']

# Or

>>> list(map(''.join, map(sorted, strs)))
['aet', 'aet', 'ant', 'aet', 'ant', 'abt']

Leave a Reply Cancel reply