2

Given:

li = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Why does print(li[-5:0]) not print [d, b, c]?

-5 is the beginning and 0 is the end, so, why does print(li[-5:0]) print an empty list?

Shouldn't print statement start from d which is -5 and go through until 0+1 which is c?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
yigitta
  • 21
  • 1
  • 4
    [**We don't allow images of text (code/input/output/errors, or otherwise) on Stack Overflow**](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). Please post all text into the question with [correct formatting](https://stackoverflow.com/editing-help). Questions with images of text/code/errors are routinely closed. Please also check out the [tour](https://stackoverflow.com/tour) and the [question guide](https://stackoverflow.com/help/how-to-ask) to make sure this & your future questions are suitable for this Q&A. – costaparas Feb 04 '21 at 13:57
  • 2
    Did you mean `-5:0:-1` instead? From your question it's not entirely clear what you mean. Please add the desired/expected output as well. – a_guest Feb 04 '21 at 13:59
  • If you use slicing as `list[start:stop]` and don't provide a `step` parameter, it will select from *start* through *stop-1*. In your case, `start > stop`, so it selects nothing. – Primeval Coder Feb 04 '21 at 14:08
  • Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – MisterMiyagi Feb 04 '21 at 14:14
  • 1
    Why do you expect `[d, b, c]`? Shouldn't it be `[d, c, b]`? – Tomerikoo Feb 04 '21 at 14:15
  • If you have these indexes in variables expressing negative indexing you can do this: `li[start:end or None]` – Alain T. Feb 04 '21 at 23:11

1 Answers1

2

Simply because you didn't define a step, as seen in these docs

step Optional. Extended slice syntax. Step value of the slice. Defaults to 1.

So because you don't have a negative step to tell it to step backwards, it doesn't

Additionally -5 refers to the 5th element from the end, which is greater than 0 so because its already at an index greater than its end destination, there is nothing to do.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 1
    the [url](https://python-reference.readthedocs.io/en/latest/intro.html) points to a resource that has not been updated in last 6 years! Is it possible to provide a latest resource? Furthermore, the documentation is for python 2.x only. – mnm Feb 04 '21 at 14:17
  • @mnm - Its not been updated in 6 years because theres no need to update the page at all, I've also quoted the one important part on that page so there isn't any real need for the link to be there other than to provide credit – Sayse Feb 04 '21 at 14:22
  • 1
    I now see the point. Thanks for the clarification. – mnm Feb 04 '21 at 14:33
  • With `li = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']` To slice backwards from `'d'` though and including `'a'` you would do `li[-5::-1]` – dawg Feb 04 '21 at 17:42