Skip to content Skip to sidebar Skip to footer

Python: How Do I Reverse Every Character In A String List?

For instance, myList = ['string1', 'string2', 'string3']... now I want my result to be this: ['1gnirts', '2gnirts', '3gnirts'] I've found few examples on how to reverse string

Solution 1:

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

Solution 2:

There are two things you need to use, the reverse function for lists, and the [::-1] to reverse strings. This can be done as a list comprehension as follows.

myList.reversenewList= [x[::-1] for x in myList]

Solution 3:

Or

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

Solution 4:

If you already know how to reverse a single word, for this problem you only have to do the same for each of the words of the list:

defreverseWord(word):
    # one way to implement the reverse word functionreturn word[::-1]

myList = ["string1", "string2", "string3"]

# apply the reverseWord function on each word using a list comprehension
reversedWords = [reverseWord(word) for word in myList]

Post a Comment for "Python: How Do I Reverse Every Character In A String List?"