5

Yeah I know there are a lot of similar questions up there. But I just cannot find what I was looking for.

My confusion is about the backward slicing.

my_jumble = ['jumbly', 'wumbly', 'number', 5]
print(my_jumble[:1:-1])

Now I have found that the result will be

[5, 'number']

So I thought that maybe I will test it out by changing the ends in that string slicing.

print(my_jumble[:2:-1])

I was really sure that Python would give me something like

[5, 'number', 'wumbly']

Instead it gave me this which made me totally lost...

[5]

Can someone explain what is going on here? I am new to Python and find this very confusing.. Thanks for any help.

4 Answers 4

8

I think one of the easiest ways to understand what is going in the code is by understanding that backward slicing reverses the way you index your list (visually, it is like reversing your list) before getting sliced but the indexes of the elements in the list themselves do not change.

Thus when you have a list like this:

['jumbly', 'wumbly', 'number', 5]
    0        1         2       3   #<-- index

by making it backward reading (adding -1 as the third indexer), you make it looks like this (because it is now indexing from the last to the first, instead of from the first to the last):

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index

and then when you slice from the "beginning" to one (:1), you get everything from the "beginning" (now the "beginning" is 3) and stop when seeing 1:

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index
 ^      ^         x
 grab! grab!      nope!

Thus you got your return:

[5, 'number']

The same principle applies when you backward slice with [:2:-1]:

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index
 ^      x
 grab!  nope!

Thus you got your result:

[5]

Now, using that principle, you know what to put as the second indexer if you want to return what you want: zero! --> [:0:-1]:

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index
 ^      ^         ^       x
 grab!  grab!     grab!   nope!

Then, you will get the result that you want:

[5, 'number', 'wumbly']
Sign up to request clarification or add additional context in comments.

5 Comments

Amazing explanation. I understand it now. Thank you so much.
@HarryLens you are welcome! ;) when we can see it graphically, things get easier to understand... :)
> backward slicing reverses your list first before getting sliced < Any docs for that? I am seeing this statement for the first time.
@warvariuc I am saying it "connotatively", perhaps I should put a note for that..
@warvariuc OK, I updated my answer.. I think it is clearer now... the "reverse" is "visually" not "actually", because it is actually the indexing which is getting reversed
1

The syntax you are using is list[start:stop:step]

If you do not input a value for start, not even zero, then python decides a suitable start. It will be 0 for a positive step and the last element for a negative step.

So in the first example, you are effectively saying choose all items starting from 0, till 1, but in the reverse order. So it printed out [5,'number']

In your second example, what you are saying is choose all items starting from the first, till the third, in the reverse order. So going in the reverse order, you start from 5, the third item in your list is 'number', but since you said only till the third, it stops right there.

Since you have given a positive value for stop, it will be in the left to right direction, hence the third element in the proper order in your case.

Also note that in python list[start: stop] is equivalent to [start: stop), the first element is considered, is exclusive of the right boundary.

Comments

1

array[start:end:step] means to start with index start then on each loop cycle add to it step and break the loop if the index becomes greater or equal to end. If start is omitted, it's equal to 0. If end is omitted, it's set to len(array). If start or end is negative, it's set to len(array) + start or len(array) + end. If step is negative, it's added to the current index on each loop cycle, but the condition to continue the loop is current_index > end, not current_index < end when step is positive.

So ['jumbly', 'wumbly', 'number', 5][:1:-1] mean to start taking elements from index len(array) to index 1 (not including) -- so we are given the items ['number', 5]:

>>> ['jumbly', 'wumbly', 'number', 5][:1:-1]
>>> [5, 'number']

['jumbly', 'wumbly', 'number', 5][:2:-1] mean to start taking elements from index len(array) to index 2 (value 'number' is stored in the list at index 2)(not including) -- so we have [5]:

>>> ['jumbly', 'wumbly', 'number', 5][:2:-1]
>>> [5]

Or to explain better with a string whose characters are the indexes:

>>> '0123'[:1:-1]
>>> '32'

>>> '0123'[:2:-1]
>>> '3'

Comments

0

@HarryLens To do what you actually want, you'll have to do it like this.

print(my_jumble[-1:0:-1])

Even this would do:

print(my_jumble[:0:-1])

I think you believed that when you stride it by a -1, the list is reversed. You see the first number represents the starting position and the second number the ending position(of the slice you want) in the current list, not the reversed list. Refer this.

2 Comments

So what is the logic here? I mean this 'print(my_jumble[:2:-1])' means print things from the start to the 3rd element backwards? I am super confused...@Dhruv
@Alan, print(my_jumble[:2:-1]) is not effectively [0:2:-1] Try it out: [0: 2: -1] will just print out []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.