howtouselinux

Understanding negative index in Python

Table of Contents

Python is a versatile language that you can use on the backend, frontend, or full stack of a web application. In this blog post, we will discuss negative indexing in Python. We will cover what it is and how to use it. Negative indexing can be confusing for beginners, but once you understand it, you will find that it is a very powerful tool. Let’s get started!

Understanding negative indexes in Python

Negative indexes are a way to allow you to index into a list, tuple or other indexable container relative to the end of the container, rather than the start.The reason this is called negative indexing is that the number you are using is less than the 0th element in the list.

They are used because they are more efficient and are considered by much more readable.

l = [0,1,2,3,4,5]
>>> l[-1]
5
>>> l[len(l) – 1]
5

Negative indexing in Python is when you use a negative number to index a list. For example, if we have a list of numbers:

>>> my_list = [0, 1, 2, 3]

And we want to get the last element in the list, we would use negative indexing:

>>> my_list[0] # Get the first element in the list
>>> my_list[-1] # Get the last element in the list

As you can see, we use the – sign to denote that we want to start counting from the end of the list.

012345
Python
-6-5-4-3-2-1

The first one will be significantly quicker.You can also use negative indexes in slices, and as with positive indexes, when you do a slice operation, the last element is not included in the slice

>>> l = [0,1,2,3,4,5]
>>> l[-3:-1]
[3,4]

As with positive indexes, the length of a slice can be calculated by subtracting the start index from the end index – in this case: −1−(−3)=−1+3=2 as demonstrated.you can even use negative indexes with negative steps to make a reversed slice :

> >>l = [0,1,2,3,4,5]
>>> l[-1:-4:-1]
[5,4,3]

here the 3rd value in the slice tells Python to go backwards from index -1 (the last element – number 5 here) to index -4 (i.e. 4th from the end – item 2 here), with the final element in the slice omitted.

use negative index in Python pop method

The pop() method takes a single argument (index) and removes the element at that index from the list. It also returns the removed element.If you don’t pass an index to the pop() method, it removes and returns the last element in the list.

>> my_list = [0, 1, 2, 3]
>>> my_list.pop() # Remove and return the last element in the list
3
>>> my_list.pop(0) # Remove and return the first element in the list
0
>>>my_list = [0, 1, 2, 3]
>>># print(my_list.pop(-1)) # Get the last element in the list

use negative index in Python insert method

The insert() method takes two arguments: the first is the index at which you want to insert the element, and the second is the value you want to insert.

>> my_list = [0, 1, 2, 3]
my_list.insert(1, 4) # Insert the element 4 at index 1
>>> my_list
[0, 4, 1, 2, 3]
>>> my_list.insert(0, -100) # Insert the element -100 at index 0
>>> my_list
[-100, 0, 11, 22, 33]
>>> my_list = [0, 11, 22, 33]
And we want to insert the element 44 at index -1
>>> my_list.insert(-1, 44)
>>> my_list
[0, 11, 22, 44, 33]

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.