79123

Python: How do I reverse every character in a string list? [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/47131096/python-function-to-reverse-strings-in-list" dir="ltr" rel="nofollow">Python - Function to reverse strings in LIST</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

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 strings in general but not specific to this problem ... I'm bit confused on how to do a reverse of strings that are in a list.

Answer1:

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

Answer2:

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.reverse newList = [x[::-1] for x in myList]

Answer3:

Or

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

Answer4:

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:

def reverseWord(word): # one way to implement the reverse word function return word[::-1] myList = ["string1", "string2", "string3"] # apply the reverseWord function on each word using a list comprehension reversedWords = [reverseWord(word) for word in myList]

Recommend

  • cannot find symbol : class java.awt.graphics [duplicate]
  • nill when assigning data label (unexpectedly found nil while unwrapping an Optional value)
  • Is it possible to have a struct which contains a reference to a value which has a shorter lifetime t
  • Rails updating attributes of a User Model from OrdersController
  • How to match a emoticon in sentence with regular expressions
  • PHP html_entity_decode Doesn't Decode Entity as Expected?
  • WorkManager Data.Builder does not support Parcelable
  • SQL EXCEPT performance
  • How to make a Node from a String?
  • Reference the second row in named table for data validation
  • PHP: concatenation of multidimensional array elements
  • How to compare Strings for Percentage Match using vb.net?
  • How to convert list into string with quotes in python
  • Why == is different for Integer and String?
  • How can I write a recursive function to reverse a linked list?
  • Binary Tree Traversal Sum Of Each Depth
  • replacing while loop with list comprehension
  • Best win32 compiled scripting language? [closed]
  • help('modules') crashing? Not sure how to fix
  • OpenCV Python: Draw minAreaRect ( RotatedRect not implemented)
  • pymongo replication secondary readreference not work
  • import scipy.sparse failed
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Asynchronous UI Testing in Xcode With Swift
  • Excel - Autoshape get it's name from cell (value)
  • Submit form in a displaytag pagination
  • How can I use Kendo UI with Razor?
  • ActionScript 2 vs ActionScript 3 performance
  • php design question - will a Helper help here?
  • Matrix multiplication with MKL
  • AngularJs get employee from factory
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?