+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  but an error for anything else, why? Code: num = [5, 4, 3, , 1] print(num) Output: 2

+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

+5

You have nested lists. In num there is a list with one object in it at the position 3 while all other objects of num are integers. In order to get the integers out of num you have to address them with num[index]. If you address element 3 of num with num you will be given not an integer value but the list which is located there. To get the value 2, what is the element with the index 0, you have to address this with num. If you address other elements of num like this you get an error because this level is not available.

+1

Index 3 accesses the 4th item of the list: num gives  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 gives 2 any other index thant 0 does not exists in the inner list, so it raises an error.

+1

Rayyan At index 3 there is a list so it can also be indexed . Whereas other are integers you cannot index them..

+1

go to the ict teacher