+ 3
HOW does this work? In indexing a list, if i give give two index parameters like in the code given below:
i am getting an output if i give the second parameter as [0] but an error for anything else, why? Code: num = [5, 4, 3, [2], 1] print(num[3][0]) Output: 2
4 Respostas
+ 3
Rayyan
At index 3 there is a list so it can also be indexed . Whereas other are integers you cannot index them..
+ 1
Index 3 accesses the 4th item of the list:
num[3] gives [2] which is itself another list
This other list can also be idexed
but it has only one element, so the only existing index possible is 0.
that is why:
num[3][0] gives 2
any other index thant 0 does not exists in the inner list, so it raises an error.
+ 1
go to the ict teacher