In the context, why can't I cancel the list by skipping the last item in the same bracket?

In Python, I can install endwhich I want in the snippet:

l = [0, 1, 2, 3, 4, 5]
l[:-1] = [0, 1, 2, 3, 4]

I can also set the necessary step:

l[::-1] = [5, 4, 3, 2, 1, 0]

So why can't I cancel a list that skips the last item at a time? I mean why this is happening:

l[:-1:-1] = []

To get the expected result using slices, I have to do:

l[:-1][::-1] = [4, 3, 2, 1, 0]

Does this have anything to do with field priority? The order in which actions occur during slicing?

+4
source share
1 answer

You can, but you have to do it like this:

>>> x[-2::-1]
[4, 3, 2, 1, 0]

, , , "" , "" . , , , , , , .

, , slice [a:b:-1] " [a:b], , ", , . , . , ; , , , , .

+9

Source: https://habr.com/ru/post/1616004/


All Articles