Pop Item from Python List
Popping item from a Python List mean, deleting the last object from the list and returning the deleted object.
Pop operation reduces the length of the Python List by 1.
Pop on an empty list causes IndexError
.
Example 1 – Pop item from Python List
In the following Python program, we initialize a list and pop an item from the list.
example.py – Python Program
#initialize list aList = [True, 28, 'Tiger', 'Lion'] #pop item from list item = aList.pop() #print popped item print('Popped item is :', item) #print list print(aList)Try Online
Output
Popped item is : Lion [True, 28, 'Tiger']
Example 2 – Pop item from Empty List
We shall initialize an empty list and try popping.
example.py – Python Program
#initialize list aList = [] #pop item from list item = aList.pop() #print popped item print('Popped item is :', item) #print list print(aList)Try Online
Output
Traceback (most recent call last): File "example1.py", line 5, in <module> item = aList.pop() IndexError: pop from empty list
Conclusion
In this Python Tutorial, we learned how to pop an item from the end of list using pop() with the help of example Python programs.