Register Login

Indexerror: pop index out of range

Popping elements using the index value is an important method available with the different iterable objects. But, if we do not handle this pop() method properly, we might encounter the Indexerror: pop index out of range error. In this article, you will learn about dealing with the Indexerror: pop index out of range error and will also understand when it will occur.

The pop() method:

For eliminating an element from an array, developers can also use the pop() method of the array. If we simply use pop() without passing any parameter, then it will delete the element from the last index. But if we specify the index value of the array, it will remove that particular element from the array. The syntax for pop() looks something like this:

array_object.pop(optional_parameter)

What is Indexerror: pop index out of range error?

It is a type of error that occurs when programmers try to eliminate or remove an element that is not within the bounds of the object. Let suppose, your iterable object has 7 elements, and you wish to remove the 9th element from the object. Obviously, we can understand that this will not be possible because there isn't any such element location existing. The overall size of that iterable object is 7 and trying to remove the 9th element seems wrong and silly. In that kind of situation, you will encounter Indexerror: pop index out of range error.

Example:

indexList = [2, 3, 4, 5, 6, 7, 8]
print(indexList)
print(" Popping The elements.... ")
indexList.pop(3)
indexList.pop(10)
print(" After popping the elements " , indexList)

Output:

[2, 3, 4, 5, 6, 7, 8]
 Popping The elements....
Traceback (most recent call last):
  File "testprogram.py", line 5, in <module>
    indexList.pop(10)
IndexError: pop index out of range

Indexerror: pop index out of range

Explanation:

We have to count the number of elements present in the iterable object. Then, we have to use the pop() and pass an argument that does not exceed the upper bound of that object element. Now, we are popping the third and the 10th element. Although, the third element will be popped up because it resides within the upper bound. But, there is no 10th element in that list. We don’t have the 10th element still we are trying to pop() it out of the list – which is not possible. This is the reason why we have enoundered the “Indexerror: pop index out of range” error.

Solution:

There are two ways to solve this problem. These are:

Method 1: Calculating the upper bound of the iterable object:

We have to count the number of elements present in the iterable object. Then, we have to use the pop() and pass an argument that does not exceed the upper bound of that object element. Once, we count the maximum element of that object, we can pop elements by passing the index value that is within that upper bound.

Program:

indexList = [2, 3, 4, 5, 6, 7, 8]
print(indexList)
print(" Popping The elements....")
indexList.pop(3)
indexList.pop(5)
print(" After popping the elements", indexList)

Output:

[2, 3, 4, 5, 6, 7, 8]
 Popping The elements....
 After popping the elements [2, 3, 4, 6, 7]

Explanation:

Here, we have taken a list by the name indexList and stored seven elements into it. Next, we print the complete list as it is. Now after popping the element having index 3 and element having index 5, we again display the list; but this time we have taken care of the upper bound and that is why it won’t show any error.

Method 2: Appending more elements to increase the upper bound of the object:

We can increase the number of elements of the iterable object if we want to pop() the nth element from the object. That way, the pop() will find itself within the range of the upper bound.

Program:

indexList = [2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16]
print(indexList)
print("Popping The elements....")
indexList.pop(3)
indexList.pop(10)
print("After popping the elements", indexList)

Output:

[2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16]
Popping The elements....
After popping the elements [2, 3, 4, 6, 7, 8, 9, 10, 12, 14, 16]

Explanation:

Here, we have taken a list by the name indexList and stored seven elements into it. Next, we print the complete list as it is. Now after popping the element having index 3 and element having index 5, we again display the list; but this time we have increased the overall length of the iterable object (here list) and appended more elements to it to avoid the Indexerror: pop index out of range error. Finally, we display the result. 

Conclusion:

Both solution techniques work fine, but the first one is preferred in most of the scenarios. It is essential for a programmer to understand the upper bound and lower bound of an element before popping any item from that iterable object.