Reputation: 656
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.
Upvotes: 5
Views: 3823
Reputation: 59584
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'
Upvotes: 1
Reputation: 30813
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']
Upvotes: 8
Reputation: 1703
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.
Upvotes: 1
Reputation:
@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.
Upvotes: 0