Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
last index
#2
Think of the indexes as being between the items, rather than on the items:

Output:
lunch = ['Spam', 'spam', 'spam', 'eggs', 'spam'] ^ ^ ^ ^ ^ ^ | | | | | | 0 1 2 3 4 5 -5 -4 -3 -2 -1
If you just give one index, you get the item after it. That's why -1 gets you the last item. But if you give two indexes, you get everything between them. That's why 1:-1 gets you everything but the first and last item.

BTW, Guido didn't come up with the idea. It's been around since the sixties, at least in computer science. There's more about the reasoning at Wikipedia.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
last index - by paul18fr - Jul-31-2019, 08:35 AM
RE: last index - by ichabod801 - Jul-31-2019, 01:13 PM
RE: last index - by paul18fr - Jul-31-2019, 01:27 PM
RE: last index - by perfringo - Jul-31-2019, 05:10 PM
RE: last index - by paul18fr - Jul-31-2019, 08:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Getting Index Error - list index out of range krishna 2 2,714 Jan-09-2021, 08:29 AM
Last Post: buran
  Getting Index Error - list index out of range RahulSingh 2 6,266 Feb-03-2020, 07:17 AM
Last Post: RahulSingh

Forum Jump:

User Panel Messages

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