+ 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
+ 7
Data types for Index0 = integer Index1 = integer Index2 = integer Index3 = list (list of list) Imdex4 = integer You can only use [] for list so an error would occur if you try it on any other datatypes in the list above
20th Oct 2019, 10:46 AM
Mirielle
+ 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
+ 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é
+ 1
go to the ict teacher
22nd Oct 2019, 11:41 AM
Codey Thomas :)