7
a='0123456789'

>>> a
'0123456789' 

>>> a[1:-6:1] # working as expected
'123'

>>> a[2:-1:-1] # i was expecting '210' as answer based on start index=2 end index=0
''

Please help in understanding how the second slice operator returned nothing

arshovon
  • 13,270
  • 9
  • 51
  • 69
sai dattu
  • 71
  • 3
  • It is not possible to decrement from 2 to -1 because that is near the end. – QuentinUK Aug 21 '18 at 06:02
  • There is a very good post about python slice - https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation – mta194 Aug 21 '18 at 06:04

5 Answers5

8

Negative indices for start and stop are always converted by implicitly subtracting from len(sequence). So a[2:-1:-1] translates to a[2:len(a)-1:-1], or a[2:9:-1], which reads in English as "start at index 2, and go backwards by one until you're at or below index 9".

Since you start at index 2, you're already at or below 9, and the slice ends immediately.

If you want to slice from index to back to the beginning of the string, either omit the end index (which for negative slice means continue until "beginning of string"):

a[2::-1]

or provide it explicitly as None, which is what omitting stop ends up using implicitly:

a[2:None:-1]
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • My understanding on slice is a[start:end-1:step(+ve)] a[start:end+1:step(-ve)] . was it wrong assumption? – sai dattu Aug 21 '18 at 07:26
  • @saidattu: It was a wrong assumption. [Understanding Python's slice notation](https://stackoverflow.com/q/509211/364696) gives a more complete explanation. – ShadowRanger Aug 21 '18 at 12:39
  • This is what I have been looking for, 'implicitly subtracting from len(sequence)'. It would be very appreciate if someone leave the resource of this part (for example, Python official doc or a source on Python official Github repository). I failed to find it. – Park Mar 17 '23 at 01:48
  • @Park: Probably the most beginner friendly description comes from [the Programming FAQ: What's a Negative Index?](https://docs.python.org/3/faq/programming.html#what-s-a-negative-index) – ShadowRanger Mar 17 '23 at 01:51
  • @ShadowRanger How kind you are! Thank you very much. – Park Mar 17 '23 at 03:35
4

I think, you want to do something like this:

a[2::-1]  # produces: [2, 1, 0]

Slicing from 2 to -1 with step -1 (what you did) does yield an empty list because you want to move forward with negative step size and hence you receive an empty list.

This SO post provides the explanation:

It's pretty simple really:

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:end:step] # start through not past end, by step

The key point to remember is that the :end value represents the first value that is not in the selected slice. So, the difference beween end and start is the number of elements selected (if step is 1, the default).

The other feature is that start or end may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

tafaust
  • 1,457
  • 16
  • 32
  • My understanding on slice is a[start:end-1:step(+ve)] a[start:end+1:step(-ve)] . was it wrong assumption? – sai dattu Aug 21 '18 at 09:07
  • @saidattu What is `ve` in this context? – tafaust Aug 21 '18 at 10:54
  • My understanding on slice is a[start:end-1:step(possitive)] a[start:end+1:step(negative)] . was it wrong assumption? – sai dattu Aug 21 '18 at 11:35
  • @saidattu I don't understand your specific question. Given `a = [1, 2, 3, 4, 5]`, if you want to slice a list from left to right: `a[ 0 : 3 : 1 ]` gives `[0, 1, 2]` and from right to left `a[ 3 : 0 : -1 ]` gives `[3, 2, 1]`. If you want to learn more about the behavior of python slices, just open up a python interactive session and test it out yourself. – tafaust Aug 21 '18 at 12:02
0

You could just do:

a=[0,1,2,3,4,5,6,7,8,9]
print(a[2::-1])

Because when you do -1 in a slice operation (as in your code), python assumes it as end index position.

To prove this:

>>> a[-1]
9

Your code is of the format:

a[start:end:step]

start in your case should be 2 (inclusive) and end should be blank (exclusive). When you specify step as -1, you slice in reverse order.

Austin
  • 25,759
  • 4
  • 25
  • 48
0

Slice index looks like picture below. The slice s[start:end] is the elements beginning at start and extending up to but not including end. So using -1 means that the last index is not included. So this is working as designed. If you want the last index, you can choose to leave it blank.

Slice Index

So

  • s[1:] return ello
  • s[1:-1] return ell

See Google Python Class - Strings which has inspired this answer and from where I snagged the picture.

Jay Rajput
  • 1,813
  • 17
  • 23
0

A negative index is always interpreted as an index from the end. None, on the other hand always gets interpreted as "until the appropriate end":

>>> a[2::-1]
'210'

A missing index is equivalent to None in a slice:

>>> a[2:None:-1]
'210'

To see what I mean by the appropriate end, try

>>> a[:2:-1]
'9876543'
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264