pop() method is used to remove an item from a list. Python
pop() takes the index of the item you would like to remove and returns the removed item.
In programming, when you are working with lists, 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.
The Python language includes a built-in function that can be used to remove an element from a list:
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.
In Python, lists are indexed, which means that each individual item in a list has its own value. These index numbers start from
. Lists can contain any Python data type, such as strings, numbers, and dictionaries.
pop() list method allows you to remove a specific element from a list by using its index number to state which element you want to remove. The syntax for the
pop() method is as follows:
pop() takes one parameter, which is the index position of the item you want to remove from your list.
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 in a Python program. Say that we are building an application that manages the teas sold at our cafe. We have run low on Earl Gray tea, and we want to remove it from our tea list. Here’s a program we could use to remove Earl Gray 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']
Let’s break down our code. On the first line, we declare a list of teas that contains five values. Then, we use the
pop() method to remove the tea at the index position
teas array. In this case, that tea is Earl Gray.
Finally, we print out both the tea that we have removed and our new list of teas.
pop() can process negative index values. Say that you have received a delivery from your supplier of Earl Gray, 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.
Python Pop With No Item
When you use the
pop() method on a list and refer to an item that does not exist within that list, an error will be returned. This is because
pop() cannot find the item to which you are referring. 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() 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
pop() works with negative index numbers and discussed what happens when you try to remove an item from a list that does not exist.
You’re ready to start working with the
pop() method in Python like an expert!