Negative Index
noun
say it: “NEG-uh-tiv IN-deks” /ˈnɛɡətɪv ˈɪndɛks/
A position in a list or string counted from the end: -1 is the last item, -2 the one before it, and so on.
What is a negative index?
A negative index counts from the end of a list: -1 is the last item, -2 the one before it.
queue = ["Ana", "Ben", "Cai"]
print(queue[-1]) # Cai
print(queue[-2]) # Ben
In a sentence
- “"report.pdf"[-3:] uses a negative index to get the file's last three letters.”
- “The negative index -1 gave the last person in the queue.”
Level: Apprentice · More Python terms at this level
Updated