+ 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

20th Oct 2019, 10:21 AM
Rayyan
Rayyan - avatar
4 Réponses
+ 3
Rayyan At index 3 there is a list so it can also be indexed . Whereas other are integers you cannot index them..
22nd Oct 2019, 7:23 AM
Indira
Indira - avatar
+ 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.
20th Oct 2019, 3:09 PM
Loïc Mahé
Loïc Mahé - avatar
+ 1
go to the ict teacher
22nd Oct 2019, 11:41 AM
Codey Thomas :)
Codey Thomas :) - avatar