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 ?

Thanks

Upvotes: 0

Views: 49

Answers (2)

I'mahdi
I'mahdi

Reputation: 24049

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']

Upvotes: 4

Anmol Parida
Anmol Parida

Reputation: 688

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

Upvotes: 1

Related Questions