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.

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.
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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 –