0

I have been stuck on understanding this slice operation for a couple of hours. Small insignificant but I can't get past it.

MyList = ['blue', 'red', 'yellow', 'white', 'orange']

MyList[-1:1:-1]

and it returns the following:

['orange', 'white', 'yellow']

I am not understand how the first is '-1' and second is '1' with interval of '-1' and it returns [4][3][2] or [-1][-2][-3].

  • In slicing, the first argument is the starting point (-1 means last, -2 second last, ...). The second argument is the ending point (not included), and the third optional argument is the step (-1 means to go in the reverse way). So, the first element is `'orange'`, 2nd `'white'`, 3rd `'yellow'`, and it stops here since the index of `'red'` is 1, and 1 is not included. – PhysicistSouravDas Sep 06 '21 at 18:15
  • Sorry I meant if I do [-1:1:1] it returns nothing, if I do [-1:1: ] it returns nothing. I am not understanding why [-1:1:-1] returns but the other two examples don't. – user16845529 Sep 06 '21 at 18:48

0 Answers0