Positive and negative indices in Python?


By indexing, we can access items in Python sequence data types. Strings, lists, tuples, and range objects are sequence data types. In this tutorial, we'll go over-indexing in further detail.

What Are List Indexes?

Any linear data structure in any programming language is built around indexing. The default index for each machine starts at 0 and continues up to n-1. In this case, n represents the overall number of items in the corresponding data structure. Types include

  • Positive indexing − Increases from 0 to 1.

  • Negative indexing − each traversal moves from tail to head, starting with the last element.

These facilitate our ability to access the many components of this data structure. Let's examine the procedures in the next part.

Tuple Indexing

Similar to how we access elements in lists and strings, we can access elements in tuples. So, indexing and slicing are the only methods we need to access items. Additionally, indexing is straightforward, beginning at index zero, just like in lists. In addition, the figure we put within the square bracket represents the tuple's index. Let's see a few instances of tuple indexing being used to retrieve a tuple's elements.

Example 1

tup1 = (10, 3, 4, 22, 1)
# for accessing the first element of the tuple
print(tup1[0])

Output

10

Example 2

tup1 = (10, 3, 4, 22, 1)
# accessing the third element of the tuple
print(tup1[2])

Output

4

Example 3

tup1 = (10, 3, 4, 22, 1)
print(tup1[10])
# gives an error as the index is only up to 4

Output

IndexError: tuple index out of range

Example 4

tup1 = (10, 3, 4, 22, 1)
print(tup1[1+3])
# the expression inside the square brackets results in an integer index 4. Hence, we get the element at the 4th index.

Output

1

Zero-Based Indexing in Python

In Python, positive zero-based indexing is the fundamental method for accessing iterable items.

As a result, an index starting at 0 may refer to any element in the iterable.

The first element in zero-based indexing has an index of 0, the second has an index of 1, and so on.

Example 5

myList = [1, 2, 3, 4, 5]
# NORMAL INDEXING
print(myList[0])
print(myList[1]) 
print(myList[2])
# NEGATIVE INDEXING (STARTS FROM THE LAST ELEMENT IN THE LIST)
print(myList[-1])
print(myList[-2])
print(myList[-3])
print(myList[-3:])

Output

1
2
3
5
4
3
[3, 4, 5]

Negative Indexing

If we wish to print the components starting at the end, we may also use negative indexes. Additionally, by specifying the index number with a negative sign, we may carry out negative tuple indexing (-). As a result, this suggests that the compiler starts thinking about the elements in reverse order, beginning with the element that comes last in the tuple.

Example 3

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup[-4])

Output

2

Now, we can use negative indexes in slicing also.

Example 4

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup[-4:-1])

Output

(2, 56, 890)

Tuple index() Method

The tuple index() function aids in locating an element's index or location within a tuple. Essentially, this function serves two purposes −

Giving the tuple's first instance of each element.

If the indicated element is absent from the tuple, throwing an error.

Syntax

tuple.index(value)

Example 5: Removing Elements With Negative Index

Using the pop() function and giving -1 as a parameter inside it, we can remove the last element of that list, and we get a new list.

my_list = [45, 5, 33, 1, -9, 8, 76]
my_list.pop(-1)
print(my_list)

Output

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

Example 6: Finding the index of an element

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup.index(45))
print(tup.index(890))
#prints the index of elements 45 and 890

Output

2
7

Advantages of Using Negative Indexing in Python List

  • Fewer lines of code are needed, and a reversal is done in one line.

  • Simplifies difficult processes.

  • Operates quickly while requiring little complexity

Conclusion

In summary, Python allows positive indexing starting at zero and negative indexing starting at -1. In Python, negative indexing denotes that the indexing process begins at the end of the iterable. The final element is located at index -1, the next-to-last at index -2, and so on. Negative indexing is supported in arrays in the Python computer language but not in most other programming languages. This means that the index value of -1 offers the final element, and -2 gives the second last element of an array. The array's end is where the negative indexing begins.

(2, 56, 890)

Updated on: 05-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements