The Python pop() method removes an item at a specified index value from a list. This method returns the item you have removed so you know what modification has been made to your list. pop() accepts one argument: the index of the item you want to remove.
You may decide that you want to remove a specific item from a list. For example, say you are writing a program that stores a list of books available in a library. When a book is checked out, you may want to remove and return the book from your list of available books.
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
The Python language includes a built-in function that can be used to remove an element from a list: pop(). The pop() method removes an element from a specified position in a list and returns the deleted item.
In this tutorial, we are going to discuss the Python pop() method, how it is used, and what arguments the function accepts. Then, we’ll explore a few examples to show how the method can be used with lists.
Python pop() Method
The Python pop() method removes an item from a list at a certain index position. pop() accepts one argument, which is the index position of the item you want to remove:
The pop() function takes in an index value, then checks whether the item exists in the list. If the item does exist, the method will remove the item from the list and return the item it has removed. If the item does not exist, an error will be returned.
Let’s use an example to showcase the pop() function and how it removes and returns the item at the index passed through the function.
Python List pop() Example
Say that we are building an application that manages the teas sold at our café. We have run low on Earl Grey tea, and we want to remove it from our tea list. Here’s a program we could use to remove Earl Grey from our list of teas while we are out of stock:
teas = ['Earl Gray', 'Oolong', 'English Breakfast', 'Chai', 'Chamomile'] remove_earl_gray = teas.pop(0) print(remove_earl_gray) print(teas)
Our program returns the following:
Earl Gray ['Oolong', 'English Breakfast', 'Chai', 'Chamomile']
On the first line of our code, we declare a Python variable. This variable is a Python array that contains five tea names. Then, we use the pop() method to remove the tea at the index position from our teas array. In this case, that tea is Earl Grey.
Finally, we print both the tea that we have removed and our new list of teas to the Python shell.
In addition, pop() can process negative index values. Say that you have received a delivery from your supplier of Earl Grey, but you are now low on Chamomile tea. You could use the following code to remove Chamomile from your list of teas:
teas = ['Earl Gray', 'Oolong', 'English Breakfast', 'Chai', 'Chamomile'] remove_chamomile = teas.pop(-1) print(remove_chamomile) print(teas)
Our code returns:
Chamomile ['Earl Gray', 'Oolong', 'English Breakfast', 'Chai']
We used the same code as we did in our first example. The only difference is that we have specified the index value -1, which refers to the last item in our list. So, our code removed Chamomile from our list of teas.
pop() Python: Removing a Non-Existent Value
An error will be returned if you try to remove an item that does not exist.
This is because pop() cannot find the item to which you are referring and so pop() cannot return any value. Here’s what happens when we try to remove a tea at the index position 6 from our list of teas, which does not exist:
teas = ['Earl Gray', 'Oolong', 'English Breakfast', 'Chai', 'Chamomile'] remove_tea = teas.pop(6) print(remove_tea)
Our code returns:
IndexError: pop index out of range
pop() returns an “IndexError: pop index out of range” error if you specify an index value that does not exist in the array.
To handle this issue, you could use a Python try…except block. Specify your pop() method in a “try” block and if your code does not work then the contents of the “except” block will run:
try: names = ["Lewis"] names.pop(1); except: print("This value does not exist.")
In this example, we use a try…except block to process our pop() statement. Our code returns:
This value does not exist.
This message is printed to the console because the code in our “try” block raised an error. In this case, we tried to remove the item at the index position 1 in the list “names”. But, the list only has one item, whose index position is 0.
The “IndexError: pop index out of range” error is similar to the “list index out of range” error. The list index out of range error occurswill see if you try to access an item that does not exist in a list. Read our guide on the Python list index out of range error for more information.
The Python pop() method allows you to remove an element from a specified position in a list. This method is useful if you have an existing array that contains a value you want to remove.
In this tutorial, we walked through how the pop() method can be used in Python to remove items from a list. We also explored how to handle the “IndexError: pop index out of range” error.
For guidance on top Python learning resources, books, and online courses, check out our How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.