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

(Python) How do I reverse characters of a list item

I want to reverse character order of every item in a list

I have myList = ['78', '79', '7a'] and I want it to get the output 87 97 a7

so far I’ve tried:

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

newList = [x[::-1] for x in myList][::-1]

and

def reverseWord(word):
    return word[::-1]

myList = ['78', '79', '7a']

newList = [reverseWord(word) for word in myList]

this would either return the original list or reverse the entire list and not just the items

>Solution :

In your line [x[::-1] for x in myList][::-1], the final [::-1] does reverse the list, you don’t need it

What you missing is only formatting : join the element using a space

myList = ['78', '79', '7a']
res = " ".join(x[::-1] for x in myList)
print(res)  # 87 97 a7
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