-7

So if I had a list of strings such as

a = ['apple', 'banana', 'orange']

how would I reverse each string in that list to make

a = ['elppa', 'ananab', 'egnaro']

Ma0
  • 14,712
  • 2
  • 33
  • 62
rich
  • 23
  • 2
  • 7

2 Answers2

2

How do you reverse single string?

word = 'apple'
print(word[::-1])

So

words = ['apple', 'banana', 'orange']
reversed_words = [word[::-1] for word in words]
sangheestyle
  • 977
  • 2
  • 16
  • 26
0

This is how:

a = [y[::-1] for y in a]
print(a)  # ['elppa', 'ananab', 'egnaro']

Do invest some time reading about slicing

Community
  • 1
  • 1
Ma0
  • 14,712
  • 2
  • 33
  • 62