-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
  • 15,057
  • 4
  • 35
  • 65
rich
  • 23
  • 2
  • 8

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
  • 1,037
  • 2
  • 16
  • 28
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
  • 15,057
  • 4
  • 35
  • 65