Python Forum
negative indices in lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
negative indices in lists
#1
This isn't technically homework, as I'm working my own way through through a beginner's Python book rather than taking a course, but it seemed like the most appropriate thread for my question. If I'm out of line, please let me know and I won't make the mistake again.

Anyhow, I'm confused about an apparent conflict between the book's explanation of negative list indices and its subsequent use of said indices. The book is https://automatetheboringstuff.com/#toc . It's nice and clear, so I'm very happy with it, but am up against a roadblock on this one issue.

Here's the problem: I'm on the lists chapter right now, and it says "The integer value -1 refers to the last index in a list, the value -2 refers to the second-to-last index in a list, and so on." It then gives the following example list...
spam = ['cat', 'bat', 'rat', 'elephant']
...and says that spam[-1] refers to the last value, 'elephant'.

OK, so far so good. Everything's consistent. But then the book moves on to slices, and using the exact same example list that has elephant as its fourth (last) item, gives the following example:
>>> spam[0:-1]
['cat', 'bat', 'rat']
So how come "elephant" isn't included at the end? Shouldn't the result be ['cat', 'bat', 'rat', 'elephant']? Because if -1 always refers to the final list item, a result of ['cat', 'bat', 'rat'] doesn't make sense--to me, anyway. What am I missing?
Reply
#2
Doh! Sorry, I just reread the book's explanation. The only previous programming experience I have is with a macro language in which array slices include the index you specify at the end, so I kind of skimmed the array material and missed the earlier sentence that says, "A slice goes up to, but will not include, the value at the second index." My bad, and apologies for having wasted peoples' time.
Reply
#3
the negative index in your case goes from -4 to -1. To encompass the whole list it would have to go to zero. However you cannot do this with negative indexes. The way to display this would be:

spam[-4:]

It is easier to use the positive indexes.

John
Reply
#4
(Sep-13-2019, 02:19 AM)jsira2003 Wrote: It is easier to use the positive indexes.
I couldn't agree more. And thanks for the clarifying note about 0.
Reply
#5
This is about indices and slices.

Most important thing is that counting from the beginning indices start with 0 (0-based indexing) but counting from end indices start from -1 (not 0). Why? Because 0 and -0 are equal and Python wouldn't know is it index of the first or the last item.

Some slicing basics:

sequence[start:stop:step]

- sequence is object to be sliced
- start, stop, step are indices
- start index is included in slice, end index is first not included
- start, stop and step are separated by :
- to define stop and/or step previous indices must be defined
- start and stop indices can be omitted (but must have :), Python interprets these omissions as None (or in spoken language: 'from start' [:2], 'till end' [2:]).
- None (whether through omission or explicitly writing) is different from 0 (specific index)

>>> s = 'python'
>>> s[None:None]
'python'
>>> s[0:0]
''
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thanks for the explanation of why it starts at -1. Makes sense once you remember that -0 is nonsense.
Reply
#7
(Oct-11-2019, 08:37 AM)Ironword Wrote: Thanks for the explanation of why it starts at -1. Makes sense once you remember that -0 is nonsense.

I agree with that. But Python is language which gives a freedom of doing many things including non-obvious stuff or straightforward 'shooting yourself in foot'.

If one wants to count from end starting from 0 one must use '~' instead of '-':

>>> s = '42'
>>> lst = [1, 2, 3]
>>> s[~0]
'2'
>>> lst[~0]
3
>>> lst[~1]
2
With slice() one can observe that Python implicitly converts this to 'normal' -1 based indexing:

>>> backwards = slice(~0, ~3, -1)
>>> lst[backwards]
[3, 2, 1]
>>> backwards
slice(-1, -4, -1)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Positive and negative numbers in a row peterp 1 1,756 Oct-29-2020, 02:42 AM
Last Post: deanhystad
  insert() but with negative index naughtysensei 1 2,346 Oct-01-2020, 03:05 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020