Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

How to Use The Python List pop() Method

Python List Pop

The Python list pop() method is used to pop items from Python lists. In this article, we’ll quickly take a look at how we can pop elements from a List using pop().


Basic Syntax of Python List pop()

This is a method of the List object type, so every list object will have this method.

You invoke it by using:

my_list.pop()

This is the default invocation, and will simply pop the last item from the list.

If you want to pop an element from an index, we can pass the index as well.

last_element = my_list.pop(index)

This will pop the element at the index, and update our list accordingly. It will return the popped element, but in most cases, you can choose to ignore the return value.

Now that we’ve covered the syntax, let’s look at how we can use it.


Using Python list.pop()

Let’s look at the default case, where you simply want to pop the last element.

# Create a list from 0 to 10
my_list = [i for i in range(11)]

# Pop the last element
print("Popping the last element...")
my_list.pop()

# Print the modified list
print("List after popping:", my_list)

# Again Pop the last element
print("Popping the last element...")
my_list.pop()

# Print the modified list
print("List after popping:", my_list)

Output

Popping the last element...
List after popping: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Popping the last element...
List after popping: [0, 1, 2, 3, 4, 5, 6, 7, 8]

As you can see, the last element was indeed popped from our list.

Now, let’s consider the second type, where you want to pop elements at a particular index.

# Create a list from 0 to 10
my_list = [i for i in range(11)]

# Pop the last element
print("Popping the element at index 5...")
my_list.pop(5)

# Print the modified list
print("List after popping:", my_list)

# Again Pop the last element
print("Popping the element at index 2...")
my_list.pop(2)

# Print the modified list
print("List after popping:", my_list)

Output

Popping the element at index 5...
List after popping: [0, 1, 2, 3, 4, 6, 7, 8, 9, 10]
Popping the element at index 2...
List after popping: [0, 1, 3, 4, 6, 7, 8, 9, 10]

Here, since our original list was [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the element at index 5 was list[5], which was 5. So, this is removed, and our list is now without 5. Similarly, from the new list, we again remove the element at the second index, which is 2. Therefore, our final list is [0, 1, 3, 4, 6, 7, 8, 9, 10].

Dealing with Exceptions

The list.pop() method will raise a few exceptions if some conditions are violated.

IndexError Exception when the List is empty

When using the Python list pop() method, if our list is empty, we cannot pop from it anymore. This will raise an IndexError exception.

my_list = []

# Will raise an exception, since the list is empty
my_list.pop()

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop from empty list

Since we tried to pop from an empty list, this exception was raised, with the corresponding error message.

IndexError Exception when indexing

If the index passed to the pop(index) method lies outside the size of the list, this exception will be raised.

For example, trying to remove the 12th index element in a list of 11 elements will raise this exception.

my_list = [i for i in range(10)]

# Will raise an exception, as len(my_list) = 11
my_list.pop(12)

Output

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: pop index out of range

As expected, we did get the IndexError exception, since my_list[12] does not exist.


Conclusion

In this article, we learned how we could pop elements from a list, using the list.pop() method.

References