+ 2

Why does this python code gives an error?

list = [1,[2,3],(4,5),False,"No"] print (list[1[1]])

20th Dec 2019, 7:20 PM
Charles Xstorm Ukadike
Charles Xstorm Ukadike - avatar
4 Réponses
+ 7
The way you use the index to access the list is the issue here. Use this: list = [1,[2,3],(4,5),False,"No"] print (list[1][1]) #<-- this is ok! #print (list[1[1]]) # output = 3
20th Dec 2019, 7:26 PM
Lothar
Lothar - avatar
+ 2
You have to change it to print(list[1][1]) instead of pribt(list[1[1]])
20th Dec 2019, 7:22 PM
Abdol Hashimi
Abdol Hashimi - avatar
+ 2
Okay, thanks.
20th Dec 2019, 7:24 PM
Charles Xstorm Ukadike
Charles Xstorm Ukadike - avatar
+ 2
Just to clarify, list[1[1]] gives you an error because you're trying to access a value, in index 1, inside the value 1, when you do 1[1]. This raises an error, given that int type values do not support indexing. For the actual behavior you desire, as you already know, go with the already written answers. Happy coding!
20th Dec 2019, 8:14 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar