Leaderboard
Python Jul 24, 2026
Python Negative Indexing List Last Element

Python Negative Indexing — What does this print?

This is a daily Python challenge from the CodeShot archive. Practice your knowledge of Negative Indexing List Last Element and improve your technical interview readiness.

fruits = ["apple", "banana", "cherry"]
print(fruits[-1])
A "apple"
B IndexError
C "cherry"
D None

Detailed Explanation

Why This Question Matters

If you're just starting with Python, you'll quickly realize that lists are everywhere. Whether you're handling API responses, processing logs, or managing a queue of tasks, you'll be manipulating lists constantly.

The specific challenge here—accessing an element using a negative index—is a classic "aha!" moment for junior devs. In many other languages (like C or Java), trying to access index -1 would either throw an error or crash your program. Python does things differently. Understanding negative indexing is a shortcut that saves you from writing clunky code, and it's one of those small Pythonic features that makes the language feel much more intuitive once it clicks.

Understanding the Code

Let's look at the snippet again:

fruits = ["apple", "banana", "cherry"]
print(fruits[-1])

At first glance, it looks simple. We have a list called fruits containing three strings. Then we call print() on fruits[-1].

Here is what's happening under the hood. In Python, indexing isn't just a one-way street starting from zero. Python allows you to wrap around to the end of the sequence.

Standard indexing starts at 0 (the left side):
- fruits[0] is "apple"
- fruits[1] is "banana"
- fruits[2] is "cherry"

Negative indexing starts at -1 (the right side):
- fruits[-1] is "cherry"
- fruits[-2] is "banana"
- fruits[-3] is "apple"

Basically, when Python sees a negative number inside those square brackets, it calculates the index as length of list + negative index. In this case, 3 + (-1) = 2. And as we know, fruits[2] is "cherry".

Finding the Correct Answer

The correct answer is Option C: cherry.

Why not the others?
- Option A (apple): This would be the result of fruits[0].
- Option B (banana): This would be fruits[1] or fruits[-2].
- Option D (Error): In some languages, this would be the answer. But in Python, negative indices are a built-in feature, not a bug.

If you were thinking that -1 should represent the "first" element because it's the smallest number, that's a common trap. Just remember: negative indexing is like counting backward from the end.

Common Mistakes Developers Make

Even after you understand the logic, there are a few ways to trip up.

1. The IndexError
Negative indexing is great, but it doesn't make you invincible. If you have a list with 3 items and you try to access fruits[-4], Python will throw an IndexError: list index out of range. You can't go further back than the start of the list.

2. Confusing it with Slicing
Beginners often mix up indexing (getting one item) and slicing (getting a range). For example, fruits[-1:] (with a colon) doesn't give you the string "cherry"; it gives you a *new list* containing cherry: ["cherry"]. This is a subtle but annoying bug that can break your type-checking.

3. Off-by-one errors
When people start doing math with negative indices in larger lists, they sometimes forget that -1 is the absolute last item. They might try -0 to get the first item, but -0 is just 0, which takes you back to the start of the list.

Real-World Usage

You might be wondering, "Why not just use fruits[len(fruits) - 1]?"

You can, but that's verbose and looks like you're writing C code in Python. In a professional codebase, you'll see [-1] used everywhere. Here are a few common scenarios:

Processing File Paths
If you have a path like /home/user/documents/report.pdf and you split it by the slash, you'll get a list of folders. To get the actual filename, you just grab [-1].

Stack Operations
When implementing a stack (Last-In, First-Out), you always want the most recently added item. list.pop() does this automatically, but if you just want to *peek* at the top of the stack without removing it, my_stack[-1] is the standard way to do it.

Data Streams
When dealing with time-series data or logs, you often only care about the most recent entry. Using data[-1] is the cleanest way to grab the latest state without needing to track the exact length of the dataset.

Key Takeaways

- Python supports bidirectional indexing. You can go from the start (0, 1, 2...) or from the end (-1, -2, -3...).
- -1 is always the last element, regardless of how long the list is.
- It avoids the len() boilerplate. No need to calculate the list length just to get the final item.
- Watch your boundaries. Going beyond the list length in either direction results in an IndexError.

Why this matters

Understanding Negative Indexing List Last Element is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →