What is Negative Indexing in Python?

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[0], my_string[1]. 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 i-1.
  • Negative Index: The last character has index -1, the second last character has index -2, and the i-th last character has index -i.

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.

(Positive) Index01234567
Elementβ€˜uβ€™β€˜nβ€™β€˜iβ€™β€˜vβ€™β€˜eβ€™β€˜rβ€™β€˜sβ€™β€˜e’
Negative Index-8-7-6-5-4-3-2-1

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 IndexError.

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 append() or pop()?

Methods like append() and pop() can be used with negative indexes. For example, pop(-1) removes the last element of the list.

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 (as -0 is the same as 0 in Python).

Related Code Puzzle

Can you solve this code puzzle in our interactive puzzle app?

Negative Indexing Puzzle

πŸ§‘β€πŸ’» Are you a master coder? Test your skills now!