Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i sort for every word in a string for every string in a list?

For starters i did this

text = "Hello I want to make this code better"

x = text.split()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sorted_characters = sorted(x[5])

sortedword = "".join(sorted_characters)

print(sortedword)

I want the code to do this but i couldn’t figure out how to seperate words and every string in list:

example=['I like python', 'I like reading books']

Output should be ['I eikl hnopty', 'I eikl adeginp bkoos']

>Solution :

A one liner:
[' '.join([''.join(sorted(z)) for z in i.split()]) for i in example]

This just does what you did, with the list comperhension feature of python, and sort every element in the list

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading