I\'m slicing lists in python and can\'t explain some results. Both of the following seem natural to me:
>>>[0,1,2,3,4,5][1:4:1]
[1, 2, 3]
>>>
If you are slicing this then slicing will look like this [start:end:step]
. For this one:
>>> [0,1,2,3,4,5][1:4:1]
[1, 2, 3]
It is staring from index 1 to index 3 because it exlcudes the index 4 with 1 step at a time. You are getting an empty list in the second one because you are stepping -1 from 1st index. So this will be the solution.
>>> [0,1,2,3,4,5][4:1:-1]
[4, 3, 2]
This works because you are taking an index from 4 to one with -1 step forward.
The third number in the slice is the step count. So, in [0,1,2,3,4,5][1:4:-1]
, the slicing starts at 1
and goes DOWN by 1
until it is less than 4
, which is immediately is. Try doing this:
>>> [0,1,2,3,4,5][4:1:-1]
[4, 3, 2]