Lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Lists

(Input) number = 3 things = ["string", 0, [1, 2, number], 4, 4.56] print(things[1]) print(things[2]) print(things[2][2][4]) (Output) 0 [1, 2, 3] Traceback (most recent call last): File "..\Playground\", line 5, in <module> print(things[2][2][3]) TypeError: 'int' object is not subscriptable But when the input is number = 3 things = ["string", 0, [1, 2, number], 4, 4.56] print(things[1]) print(things[2]) print(things[2][2]) print(things[4]) Output is 0 [1, 2, 3] 3 4.56 Can someone tell me why it's not working?

13th Apr 2018, 2:51 PM
Scape Goat
Scape Goat - avatar
2 Answers
+ 1
Let's look at what things[2][2][4] does: It says: Go to the element in position 2 of things (we get: [1, 2, number]) then go to the element in position 2 of that element (we get number) then go to the element in position 4 of number. There is no element in position 4 of number. That is why you get an error
13th Apr 2018, 2:56 PM
cyk
cyk - avatar
+ 1
the first bit wont work because you are trying to find an index of a 3 dimensional array when you are only using a 2 dimensional array and by trying to print things[2][2][4] it will only get as far as finding the 2nd index of the 2nd index of things which is 3 and it then tries to find the 4th index of 3 which will result in an error as an integer DOES NOT have an index as indexing can only be done with strings or arrays (lists)
13th Apr 2018, 3:03 PM
Obbu
Obbu - avatar