In Python, negative indexing lets you count backwards from the end of a list. So,
-1 is the last item,
-2 is the second to last, and so on. It’s like starting at the end of a line of people and moving backwards to find someone.
my_list = ['apple', 'banana', 'cherry'] print(my_list[-1]) # Outputs 'cherry', the last item print(my_list[-2]) # Outputs 'banana', the second to last item
Negative Indexing for Strings
You can index single characters in strings using bracket notation
my_string. The first character has an index value of 0, the second 1, and so on.
Did you ever want to access the last element in the string? Counting the indices can be a real pain for long strings with more than 8-10 characters. But no worries, Python has a language feature for this: negative indexing.
- Positive Index: The first character has index
0, the second character has index
1, and the
i-th character has index
- Negative Index: The last character has index
-1, the second last character has index
-2, and the
i-th last character has index
Instead of starting from the left, you can start from the right. Access the last character with the negative index -1, the second last with the index -2, and so on.
x = 'cool' print(x[-1] + x[-2] + x[-4] + x[-3]) # loco
Negative Indexing for Lists
Suppose, you have list
['u', 'n', 'i', 'v', 'e', 'r', 's', 'e']. The indices are simply the positions of the characters in the list.
You can learn more about how to access the last and last n characters of a list or a string in our detailed article.
🧑💻 Related Article: How to Get the Last Element of a Python List?
Frequently Asked Questions on Negative Indexing
What is negative indexing in Python?
Negative indexing in Python allows you to access elements from the end of a list or sequence using negative numbers.
-1 refers to the last item,
-2 to the second last, and so on.
How does negative indexing differ from positive indexing?
Positive indexing starts from the beginning of the list (0, 1, 2, …), while negative indexing starts from the end (-1 for the last item, -2 for the second last, etc.).
Can you give an example of when to use negative indexing?
Use negative indexing when you need to access elements from the end of a list, like getting the last item:
my_list = [10, 20, 30] last_item = my_list[-1] # 30
What happens if I use an index that is too negative?
Using an index that is more negative than the list’s length results in an
my_list = [1, 2, 3] print(my_list[-4]) # IndexError
Can negative indexing be used with all types of collections in Python?
Negative indexing works with sequences like lists, tuples, and strings, but not with non-sequential collections like sets and dictionaries.
How does negative indexing work with slicing?
Negative indexing in slicing allows you to slice from the end. For example,
my_list[-3:-1] gets elements from the third last to one before the last.
Are there any performance differences between positive and negative indexing?
No, there are no significant performance differences between positive and negative indexing in Python.
Can I use negative indexing with nested lists or arrays?
Yes, you can use negative indexing with nested lists or arrays to access elements from the end of the inner lists.
How does negative indexing interact with list methods like
What are common mistakes or errors to watch out for when using negative indexing?
Common mistakes include using an index that’s too negative, resulting in
IndexError, or forgetting that
-1 is the last item, not
-0 is the same as
0 in Python).
Related Code Puzzle
Can you solve this code puzzle in our interactive puzzle app?
🧑💻 Are you a master coder? Test your skills now!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.