Skip to Content

How to Fix – IndexError: pop from empty list

If you have been working with lists in Python, you may have encountered the IndexError: pop from empty list error. This error occurs when you try to remove an item from an empty list using the pop() method.

fix "IndexError: pop from empty list" in python

In this tutorial, we will explore the reasons why this error occurs and provide you with some solutions to fix it.

Understanding the Error

Before we dive into the solution, let’s first understand why this error occurs. The pop() method is a built-in list method that is used to remove and return an item from a list. For example, let’s take the list [1, 2, 3] and call the pop() method.

# create a list
ls = [1, 2, 3]
# pop an element from the list
val = ls.pop()
print(val)
print(ls)

Output:

3
[1, 2]

You can see that the pop() method removed the last value from the list and returned it. Notice that the list is also modified in-place.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

When you call pop() on an empty list, there are no items to remove, and Python raises an IndexError to indicate that the list is empty.

# empty list
ls = []
# pop an element
val = ls.pop()

Output:

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Cell In[2], line 4
      2 ls = []
      3 # pop an element
----> 4 val = ls.pop()

IndexError: pop from empty list

We get the IndexError: pop from empty list error. This is because we are trying to remove an element from an empty list.

Fixing the Error

To fix the “IndexError: pop from empty list” error, you need to check if the list is empty before calling the pop() method. There are several ways to do this:

1. Using an if statement

ls = []

if ls:
    ls.pop()
else:
    print("List is empty")

Output:

List is empty

In the above example, we check if ls is not empty using the if statement. If the list is not empty, we call the pop() method to remove the last item. If the list is empty, we print a message indicating that the list is empty.

2. Using a ternary operator

You can alternatively use a ternary operator as well.


ls = []
# pop if list is not empty
ls.pop() if ls else print("List is empty")

Output:

List is empty

In the above example, we use a ternary operator to check if ls is not empty. If the list is not empty, we call the pop() method to remove the last item. If the list is empty, we print a message indicating that the list is empty.

3. Using a try-except block

ls = []

try:
    ls.pop()
except IndexError:
    print("List is empty")

Output:

List is empty

In this example, we use a try-except block to catch the IndexError that is raised when we try to pop() an item from an empty list. If the list is empty, the except block is executed, and we print a message indicating that the list is empty.

Conclusion

In this tutorial, we discussed how to fix the “IndexError: pop from empty list” error in Python. We learned that this error occurs when you try to remove an item from an empty list using the pop() method. To fix this error, you need to check if the list is empty before calling the pop() method. We demonstrated three ways to do this: using an if statement, a try-except block, and a ternary operator.

You might also be interested in –


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.