0

print([2][2]) please help me understand this....

6th Aug 2019, 5:00 PM
M Dasgupta
M Dasgupta - avatar
4 Answers
+ 1
First,there is no list name. But, assuming there is one, the interpreter first looks for the item in the list at the first index typed, so for example list[2] is also a list. And then, you can specify an index with 2 others square brackets. Does it answer your question?
6th Aug 2019, 5:16 PM
Gwlanbzh
Gwlanbzh - avatar
0
This statement gives an index error. The first [2] inside the print is a valid definition of a list that has a single element. The second [2] is trying to make a slice operation of this list and tries to retrieve the element with index 2. This is not possible because it has only 1 element whose index is 0. Hence the error.
6th Aug 2019, 5:37 PM
Tibor Santa
Tibor Santa - avatar
0
Tibor Santa Oooh ok I didn't understand the statement like this...
6th Aug 2019, 5:43 PM
Gwlanbzh
Gwlanbzh - avatar
0
SpaceNerd is right, for example: lis[2] | 0 | 1 | * 2 * | lis[2][2] |0, 1, *2*| lis = [[1,2,3],[4,5,6], [7, 8, 9]] print(lis[2][2]) = 9
7th Aug 2019, 7:40 AM
Rincewind
Rincewind - avatar