Python

How to Fix pop index out of range

While working with Lists in Python, you may need to remove elements from the List. The pop() method is one way to delete an item from the List, and it returns the deleted element. If you try to delete an element that does not exist in the List, it raises an IndexError: pop index out of range. In this guide, we will discuss how to fix this error by providing three different solutions with examples.

Reproducing the Error – pop index out of range

First, we will reproduce the error and then fix it. Create a List of five industries and try to remove the 6th element (Index = 5) from the List.

# Create List of industries
data = ['Insurance','Machinery','Manufacturing','Media','Not For Profit']
print("Industries: ",data)

# Try to remove 6th element
data.pop(5)

Output

You can see that the data (List) holds five elements. The index positions will be 0, 1, 2, 3 and 4. Here, we tried to delete the element present at index – 5, which doesn’t exist, resulting in the raised error.

Solution 1: Deleting Existing Elements

If you want to delete the first and last items from the existing list of elements, you can directly specify the index position in the pop() function. By default, it will remove the last element if the index is not specified. To remove the first element, specify the index as 0.

  1. list.pop(0) – Deletes the first item from the list and returns it.
  2. list.pop() – Deletes the last item from the list and returns it.

Example:

  1. Remove the last item from the data and display the removed item along with the updated List (data).
  2. Remove the first item from the data and display the removed item along with the updated List (data).
# Create List of industries
data = ['Insurance','Machinery','Manufacturing','Media','Not For Profit']
print("Industries: ",data,"\n")

# Remove the last element
removed=data.pop()
print("Removed last element: ",removed)
print("Industries: ",data,"\n")

# Remove the first element
removed=data.pop(0)
print("Removed first element: ",removed)
print("Industries: ",data)

Output

‘Not For Profit’ is the last item removed from the list. After removing it, the list holds: ‘Insurance,’ ‘Machinery,’ ‘Manufacturing,’ ‘Media.’ After removing the first element (‘Insurance’) from the List, the final list holds: ‘Machinery,’ ‘Manufacturing,’ ‘Media.’

Solution 2: Specifying the Conditions

If you want to handle the error, use if-else conditional statements to check if the total number of elements (length of the list) is greater than the specified index. If it is true, you can delete that element using the pop() function; otherwise, statements inside the else block are executed.

Look at the structure:

Example 1

Try to delete the element present at index = 5.

data = ['Insurance','Machinery','Manufacturing','Media','Not For Profit']
print("Total number of Industries: ",len(data),"\n")

index_=5
print("Input index: ",index_,"\n")

if (len(data) > index_):
  removed = data.pop(index_)
  print(removed,"\n")
  print(data)
else:
  print('Index out of range')

Output

There are only five elements present in the List (from Index 0 to 4), Index – 5 doesn’t exist. So, the condition inside the if statement fails, and the statement inside the else block is executed – ‘Index out of range’.

Example 2

Delete the element present at index = 2.

data = ['Insurance','Machinery','Manufacturing','Media','Not For Profit']
print("Total number of Industries: ",len(data),"\n")

index_=2
print("Input index: ",index_,"\n")

if (len(data) > index_):
  removed = data.pop(index_)
  print(removed,"\n")
  print(data)
else:
  print('Index out of range')

Output

Index – 2 exists in the list, so the condition inside the if statement is satisfied, and the element present at this index is removed from the list.

Solution 3: Use try-except block

If you want to handle the error, specify the try-except blocks (similar to if-else). Place the deletion logic under try block, and specify error handling in the except block.

Look at the structure:

Example 1

Try to delete the element present at index = 7.

data = ['Insurance','Machinery','Manufacturing','Media','Not For Profit']
print("Total number of Industries: ",len(data),"\n")

index_=7
print("Input index: ",index_,"\n")

try:
    removed = data.pop(index_)
    print(removed,"\n")
    print(data)
except IndexError:
    print('Index out of range')

Output

There are only five elements present in the List (from Index 0 to 4), Index – 5 doesn’t exist. Therefore, the statements under the try block raise the error, which is then handled in the except block by displaying the message ‘Index out of range.’

Example 2

Delete the element present at index = 2.

data = ['Insurance','Machinery','Manufacturing','Media','Not For Profit']
print("Total number of Industries: ",len(data),"\n")

index_=2
print("Input index: ",index_,"\n")

try:
    removed = data.pop(index_)
    print(removed,"\n")
    print(data)
except IndexError:
    print('Index out of range')

Output

Index – 2 exists in the list, so the try block is successfully executed, and the element present at this index is removed from the list.

Conclusion

There are three ways to fix the IndexError: pop index out of range. First, we reproduce this error with an example and then fix it by handling it using conditional statements and try-except block.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain