Python List pop()
Python List pop() method
Python List pop() method removes the object at specified index in the list.
The items in the list that are present after the specified index, if any, are shifted towards left side by one position.
In this tutorial, you will learn the syntax of, and how to use List pop() method, with examples.
Syntax of List pop()
The syntax of List pop() method is
list.pop(index)
You can read the above expression as: In the list, pop the item at the specified index.
Parameters
pop() method can take one parameter. Let us see the parameter, and its description.
Parameter | Description |
---|---|
index | Optional The index position from which we need to pop out the item. The default value is -1. Meaning the index of last item in the list. |
Return value
pop() method returns the removed item.
Exceptions
pop() method raises IndexError exception if the list is empty of if the index specified is out of bounds for the list.
Please note that the pop() method modifies the original list.
Examples
1. Remove item at index = 3 in the list in Python
In the following program, we take a list my_list with some initial values. We have to remove the item at index 3 in this list.
Call pop() method on the list my_list, and pass the index as argument to the method.
Python Program
my_list = [2, 4, 6, 8, 10, 12]
my_list.pop(3) # index=3
print(my_list)
Output
[2, 4, 6, 10, 12]
The item at index 3 is removed from the list.
Explanation
[2, 4, 6, 8, 10, 12] ← given list
0 1 2 3 4 5 ← indices
↑
index = 3 ← remove item at this index
[2, 4, 6, 10, 12] ← resulting list
2. Remove last item in the list in Python
In the following program, we take a list my_list with some initial values. We have to remove the last item in this list .
Call pop() method on the list my_list, and pass no argument to the method. We have already seen in the Syntax section that the default value of index is -1, and this lets pop() method delete the last item in the list, when no index value is given as argument.
Python Program
my_list = [2, 4, 6, 8, 10, 12]
my_list.pop() # default index=-1
print(my_list)
Output
[2, 4, 6, 8, 10]
The last item in the list is popped.
Explanation
[2, 4, 6, 8, 10, 12] ← given list
-6 -5 -4 -3 -2 -1 ← indices
↑
index = -1 ← remove item at this index
[2, 4, 6, 8, 10 ] ← resulting list
3. pop() method when index is out of bounds for the list
If we given an index to pop() method that is out of bounds for the given list, say greater than the length of the list, or less than -(length of list + 1), then pop() method raises
In the following program, we shall take a list of length 6, and try to call pop() method on this list with index = 100. Meaning, we are trying to remove the element at index = 100 in the list.
Python Program
my_list = [2, 4, 6, 8, 10, 12]
my_list.pop(100) # index=100
print(my_list)
Output
Traceback (most recent call last):
File "/Users/pythonexamplesorg/main.py", line 2, in <module>
my_list.pop(100)
IndexError: pop index out of range
Since the given index argument is out of bounds for the list my_list, the pop() method raises IndexError exception.
Summary
In this tutorial of Python Examples, we learned about List pop() method, how to use pop() method to remove an item at specified index in the list, with syntax and examples.
We have seen example programs on how to pop or remove an item at specified index, pop the last item, and pop item at index out of bounds for the list.