Negative Indexing in Python List – How to Use “-1” Parameter

Negative Indexing In Python List

A Python list can have many elements, and refer to a particular element among hundreds of elements present in them, indexing is used. Indexing in Python is a way to refer to individual items by their position within a list. In Python, objects are “zero-indexed”, which means that position counting starts at zero, 5 elements exist in the list, then the first element (i.e. the leftmost element) holds position “zero”, then After the first element, the second, third and fourth place.

But what if we don’t want to iterate from the first element to the end to get the last element or we have to get the length of the list and through that get the last element, these things require a lot of effort, so here comes the negative indexing.

This tutorial will cover everything you need to know about Negative indexing in Python.

Python List Structure

A list is defined using square brackets containing elements within them. Each element is separated with the help of a comma.

Syntax:

list_name = [element_1, element_2, ..., element_n]

Example:

int_list= [3, 4, 5, 5, 6] # a list containing integers
char_list = ['a', 'b', 'c', 'd'] # a list containing characters
str_list = ["apple", 'banana', 'lemon', 'orange'] # a list containing strings

What is List Indexing?

Indexing is used to directly access the elements of choice within a list for further use. The default index starts from 0 and goes on till n-1. Here n denotes the total number of elements in the respective data structure.

Below are the types of Indexing:

  1. Positive Indexing: Starts from 0 and increases.
  2. Negative Indexing: Starts from the last element and movement are from tail to head with each traversal.
Indexing In Lists
Indexing In Lists

If you want to know more about indexing, we have a separate tutorial on that, click here to read.

Negative Indexing in List

Sometimes, we are interested in the last few elements of a list or maybe we just want to index the list from the opposite end, we can use negative integers.

The process of indexing from the opposite end is called Negative Indexing. In negative Indexing, the last element is represented by -1.

Example:

my_list = [45, 5, 33, 1, -9, 8, 76]

print(my_list[-1]) 
print(my_list[-2])
print(my_list[-3])

Output:

76
8
-9

Reversing the Python List using -1

We have a built-in function reverse() in Python to reverse a list, let’s take a look.

Example:

my_list = [45, 5, 33, 1, -9, 8, 76]

my_list.reverse()

print(my_list)

Output:

[76, 8, -9, 1, 33, 5, 45]

Here comes another option: we can use [::-1] to reverse a list without using the reverse() method.

Example:

my_list = [45, 5, 33, 1, -9, 8, 76]

my_list[::-1]

print(my_list)

Output:

[76, 8, -9, 1, 33, 5, 45] 

Removing Elements using -1

Using the pop() function while passing -1 as an argument inside it will remove the last element of that list.

Example:

my_list = [45, 5, 33, 1, -9, 8, 76]

my_list.pop(-1)

print(my_list)

Output:

76 
[45, 5, 33, 1, -9, 8] 

Advantages of using Negative Indexing in Python List

  1. Reduces the lines of code for reversing a list in Python.
  2. Makes complex operations easier.
  3. Takes minimum time and provides low complexity.

Conclusion

In indexing, we use a positive integer inside the index operator (square brackets), but we can use a negative integer to start iterating from the last element called negative indexing. Hope this tutorial helps you to understand negative indexing in Python.

Reference

https://stackoverflow.com/questions/57872755/what-is-1-in-python