2

I have some nested list whose items can be accessed normally with:

list[0][2]

However, when I try to use a variable instead:

uinput = input("Number: )
list[uinput][2]

I get the error:

TypeError: list indices must be integers or slices, not str

(I have tried it with a dictionary using the same format and it threw the same error)

Prof
  • 657
  • 1
  • 14
  • 24
  • don't call your lists `list` – Chad S. Oct 20 '15 at 19:08
  • I tend to be more descriptive but this was just an example. Is list reserved somewhere in python or is it for readability you suggest not to? – Prof Oct 20 '15 at 19:12
  • 1
    If you use list as the name of a variable, you will no longer be able to construct lists using the list(...) builtin function (because you overwrote the name) – Chad S. Oct 20 '15 at 19:13

1 Answers1

4

You're doing it right but you have to convert it to integer first.

uinput = int(input('Number: '))

Otherwise it's a string and will throw the error you saw.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82