In this Python tutorial, you will learn how to remove the element from the end of the list using pop() method of List class.

Python List – pop()

Python list.pop() method deletes the last object from the list, and returns the deleted object.

Pop operation modifies the original list, and reduces the length of the Python List by 1.

Calling pop() on an empty list causes IndexError.

Syntax of pop() method

The syntax to call pop() method on a list myList is

</>
Copy
myList.pop()

Examples

1. Pop item from a non-empty List

In the following program, we initialize a list with some elements, and pop an item from the list.

main.py

</>
Copy
#initialize list
myList = ['apple', 'banana', 'cherry']

#pop item from list
item = myList.pop()

#print popped item
print(f'popped item is : {item}')

#print list
print(f'resulting list : {myList}')

Output

popped item is : cherry
resulting list : ['apple', 'banana']

Reference tutorials for the above program

2. Pop item from an empty List

Now, we shall take an empty list, and try popping an element from this empty list.

main.py

</>
Copy
#empty list
myList = []

#pop item from list
item = myList.pop()

#print popped item
print('popped item is :', item)

#print list
print(myList)

Output

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    item = aList.pop()
IndexError: pop from empty list

Since, the list is empty, pop() method raises IndexError.

Reference tutorials for the above program

Conclusion

In this Python Tutorial, we learned how to pop an item from the end of list using list.pop() method.