To get the elements backwards, you need to pass the step as a negative value
l = [1,2,3,4]
print l[1:0:-1]
# [2]
When you say l[1:0]
Python takes the step value as 1, by default, step
is 1
.
And when step
is greater than 0 and start
is greater than or equal to stop
, the length of the slice to be returned will be set to 0. So, an empty list will be returned.
if ((*step < 0 && *stop >= *start) || (*step > 0 && *start >= *stop)) {
*slicelength = 0;
}
When you use negative indices for start
or stop
, Python adds length of the sequence to it.