Home > Software design >  Sort a string in the list alphabetacaly
Sort a string in the list alphabetacaly

Time:01-25

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

CodePudding user response:

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

CodePudding user response:

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