Negative indexing is one of the most important features of Python. It allows us to access any element at a specified place by the location. Negative indexing in Python is used to specify an element counting from the right side of the array or sequence. The negative index specifies an element before and above itself, while positive index specifies after and below itself.
Negative indexing in Python
Negative indexing, or reverse indexing, is a Python feature that allows you to retrieve the last item in a list with a negative number. You can also use it to access the first item in a sequence using positive integers and vice versa.
This might seem like an odd idea at first, but negative indexing makes sense once we realize that lists are just sequential views on their underlying data structure: arrays. And in many programming languages (including Python), array indices start at 0 by default; thus, the length of any given array is equal to its highest possible index value minus 1:
“`python
>>> my_list = [1 , 2 , 3] # initial state
>>> len(my_list) # same as length() method 5
>>> my_list[-1] # last element 4
next line
“`
What is negative index in Python?
Negative indexing is a way to access elements in a list from the end of the list. It’s commonly used to create ranges, which are sequences of integers. The range [1:5] is equivalent to the list [1, 2, 3, 4].
Negative indices start from the end of the list (index -1) and count backward toward 0 (the first element). For example if you want to print out all items from a list except for its last one you can use:
print(myList[:-1])
Why use negative indexing ?
Negative indexing is useful for slicing data in a list. Slicing data means selecting some elements from the beginning or end of the list by their integer position. For example:
>>> a_list = [1,2,3]
>>> print(a_list[-1]) # prints out 2
>>> print(a_list[-2]) # prints out 3
Working of negative indexing in Python
Negative indexing is used to access certain items in lists or other containers that are based on their position, as opposed to their “natural” ordering. For example, if you have a list of numbers like [1, 2, 3] and you want to access the third item in the list (which would be 3), you can simply use:
[1, 2, 3].index(3)
Instead of using the normal way which is called positive indexing. This would look like:
[1, 2, 3].index(0)
Syntax of Python negative indexing
Negative indexing is a method of selecting a particular item from an array by using a negative index. The indices are counted from zero, so -1 refers to the last value in the array and -2 refers to the second-to-last value. Python allows you to use negative indexing in two ways:
- To select the last item in an array:
my_array[len(my_array) – 1]
- To select the first item (at position 0):
my_array[0]
Advantages of Python negative indexing
Python negative indexing is a great option for you if you want to:
- Understand what is going on
- Implement the solution quickly, easily and correctly
- Use the solution as intended by its authors
- Read the code of your peers or predecessors with ease
- Debug existing systems without getting lost in nested loops and variable scopes that lack documentation or comments.
For example, consider this simple piece of Python code:“`python2“`
Conclusion
As a developer, you should know about negative indexing in Python. It is a useful feature that provides you with an easy way to access the elements of an array without using indexes. It can be used with both lists and arrays in Python, but not with sets.