I am new to python and one of the things every newbie do come across is the slice operator. I have a list:
li=[1,2,3,4,5,6,7]
As per my understanding calling li[:-1]
is same as calling li[0:-1]
and it is but when using it with a negative steps things do not work exactly as I thought they would. So getting to my question why the result of
print(li[:-3:-2]) # is 7
but the result of
print(li[0:-3:-2]) # is []
Looking forward to some explanation of how the negative step is being treated in this scenario.
Negative Slicing in Python Similar to negative indexing, Python also supports negative slicing. This means you can start slicing from the end of the sequence. Here the slicing starts at the index -4 which is the 4th last element of the list. The slicing ends at the last item, which is at the index -1.
So [::-1] means from 1st element to last element in steps of 1 in reverse order. If you have [start:stop] it's the same as step=1 .
The slice() method is a copying method. It does not alter this but instead returns a shallow copy that contains some of the same elements as the ones from the original array.
Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:) With this operator, one can specify where to start the slicing, where to end, and specify the step.
The key is that missing (or None) start value is not always automatically set to 0. Please read the note 5 in sequence operations for s[i:j:k]
If i or j are omitted or None, they become “end” values (which end depends on the sign of k)
To simplify the situation, consider negative step -1 instead of -2. Step -1 is often used to reverse a list.
>>> print(li[None:-3:-1])
[7, 6]
>>> print(li[0:-3:-1])
[]
The first example shows, what are the real "end values" for the slice.
So, your list is li=[1,2,3,4,5,6,7]
First, understand what exactly happens in slicing.
Slicing can't be done in backward direction without using step.
Take an example, we wan't to print [7, 6, 5, 4, 3, 2]
, we can't do this using li[-1:0]
. because, slicing can't go backward as stated (we can't go backwards from index -1 to 0), and will return empty list []
.(We have to do this using li[-1:0:-1]
)
So what happens here is:
you have a list li[x:y]
, using a negative step will swap the indexes as li[y:x]
.(Once swapping of indexes is done, you should consider it as a positive step) and then print out your output w.r.t. your provided step(only the magnitude).
Now, all that I mentioned above might seem useless, but it would help you understand next part.
So, coming to your question, when you write li[0:-3:-2]
, first,indexes are swapped. So, it is same as li[-3:0:2]
. and as I have stated above, we can't go backwards (index -3
to 0
) without using a negative step, so it returns the empty list.
but, when you give input as li[:-3:-2]
, It swaps the indexes and becomes li[-3::2]
and we now easily know what will be the output ([7]
).
So, don't consider the blank space in [:-3:-2]
as 0
but as left end.(for better understanding)
Hope this helped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With