-2

I tried to transform this sentence, "I am testing", into a list where each character is a value of the list and then return the reverse chain, like this: "gnitset ma I".

But when I try to run the program I met the error, IndexError:list assignment index out of range.

frase_lista = [""]
i = 0
for letter in "I am testing":
    frase_lista [i] = letter
    i += 1
print(frase_lista**)
Enthus3d
  • 1,727
  • 12
  • 26

2 Answers2

0

Maybe,

frase_lista = "I am testing"[::-1]    
print(frase_lista)

might be an option.


Or if you are reversing each word,

frase_lista = "I am testing".split()[::-1]

for i in frase_lista:
    print(i[::-1])

Output

gnitset
ma
I
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Thank you for your help. Both options are useful. Also, I wanted to split each letter no each word and then, save it into a list. For instance, "Hello Emma" creates a list where each value is a letter: 'H' value [0] of the list 'e' value [1] etc. Thanks for your answer again. – Miguel Esteban Capdevila Sep 15 '19 at 19:33
0

You're trying to access an element frase_lista[i] which does not exist yet with i > 0. frase_lista has only one element "" that you add at the beginning. If you want to add more elements you should do the following:

frase_lista += [letter].

Also note that this won't reverse your list. To reverse the list you would need to add all letters starting from the end of the string, not from the beginning.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29