Problem with understanding list indexing - mixing positive and negative index

a = [1,2,3,4,5,6,7,8]
a[-6:5]

how is negative indexing and positive indexing mixed up?

this operation is called list slicing
in list slicing, we usually put two indexes separated by ‘:’. all the elements between the index put on left side of ‘:’ and right side of ‘:’ will be selected and that will become the result,

In the above case, -6th index of a is → 3
and, 5th index of a is → 6

so all elements between 3 and 6 will be selected excluding 6.

so,

a[-6:5] = [3,4,5]
1 Like