Sort a string in the list alphabetacaly

Question:

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 ?

Thanks

Answers:

out = []
for val in strs:
    out.append("".join(sorted(list(val))))
print(out)
Answered By: QOPS

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']
Answered By: I'mahdi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.