Python List pop() Method

In this tutorial, we are going to learn and understand the pop method for lists in Python. We would have a look at some different examples, and cases for the pop method in the list.

The thing is that the pop method is used to remove and return the element at the specified position from the list. The index that we provide to the pop index is actually optional, and if we are not passing the index as an argument, it would remove the last item in the list. On the other hand, if the index that we are providing to the pop method, is out of range, then we would get an error, saying that the pop index is out of range.

Syntax –

list_name.pop([i])

  • The index here is actually optional, so here i is in the square brackets. You should not give that square bracket while you are calling the method.
  • Also, the argument is optional, so if you are not giving the argument, then it would remove and return the last item from the list.
  • If the argument that we are providing is out of range, then we would get an error, saying that the pop index is out of range.

Python list pop() method

Now, after understanding the syntax and some information about the argument, let’s have a look at some examples, through which, we can understand the usage of the pop method.

As you can see, in the above program, we have a simple list, and then we are trying to use the pop method. We have passed index 2 to the pop method as an argument. So, the pop method would remove the element at index 2, from the list, and return it, which is being assigned to the variable removed_element. Also, after removing the element from the list, we are printing the list. Let’s have a look at the output now –

Output –

45
List after removing element: [23, 12, 29, 15, 31]

As you can see, the element at index 2, which is 45, was removed from the list, and it is the returned value from the pop method. Also, after removing the element from the list, we are printing the list.

Now, let’s have a look at another example for the same thing, just for getting a clearer picture of the same.

numbers = [1, 2, 3, 4, 5, 6]
popped_element = numbers.pop(4)
print(“This element was popped: “, popped_element)
print(“List after pop operation:”, numbers)

As you can see, in the above example, we have a simple list, with all numbers, and then we are using the pop method, to pop the element at index 4. The pop method returns the popped element, which is assigned to the reference variable popped_element. After that, we are printing the popped element and the list after the pop operation.

What if we provide no argument for the pop method?

As we mentioned earlier, the argument index that we provide to the pop method is actually optional. This means that even if we do not provide the index to the pop method, it is fine. But if we do not provide the index, it would remove and return the last item from the list.

So, now let’s have a look at an example, through which, we would understand the same thing –

As you can see in the above program, we are simply having a list, and then we are trying to use the pop method on the list. This time, we are not passing any argument to pop, which is why the last element from the list would be removed. Let’s have a look at the output now –

Output –

31
List after removing element: [23, 12, 45, 29, 15]

As you can see, the last element, which is 31, was removed and returned by the pop method. After that, we are also printing the list, in which we can see element 31 removed.

Now, let’s have a look at another example, to get a more clear picture of using the pop method, and not providing the index as an argument.

numbers = [1, 2, 3, 4, 5, 6]
popped_element = numbers.pop()
print(“This element was popped: “, popped_element)
print(“List after pop operation:”, numbers)

As you can see in the above program, we have a simple list, and after that, we are calling the pop method, and this time, we are not giving the index as an argument. So, in such a situation, the pop method would remove and return the last item from the list. So, let’s have a look at the output of the above program.

Output –

This element was popped: 6
List after pop operation: [1, 2, 3, 4, 5]

As you can see from the output, the last element was removed from the list and returned by the pop method in this situation. Also, we are printing the list after the pop operation. So, the thing is that when we are not passing the index argument to the pop method, it would remove and return the last item from the list.

What if the index argument is out of range?

We know that the pop method removes and returns the element at the specified position from the list. Also, if you do not provide the index to the method, it would remove and return the last item from the list. But what if the index that we provide is actually out of range? Well, in such a situation, we would get an error, saying that the pop index is out of range.

Let’s have a look at a simple example, through which, we can understand the same.

As you can see in the above program, we are having a simple list, and then we have provided an index to the pop method, which is out of range. In such a situation, we are going to get an error, saying that the pop index is out of range. You can also try to have a look at the output of the above program.

Let’s have a look at another program, just to get a clearer picture of the same.

numbers = [1, 2, 3, 4, 5, 6]
popped_element = numbers.pop(100)
print(“This element was popped: “, popped_element)
print(“List after pop operation:”, numbers)

As you can see from the above program, we again have a simple list, and then we are calling the pop method, to which, we are providing some index, which is out of range. In such a situation, we would get an error, saying that the pop index is out of range.

Conclusion

In this tutorial, we learned and understood the pop method for the list. Using the pop method, we can remove the element at the specified position from the list. The method returns the popped element. Also, the index that we provide to the pop method is actually optional. If we do not provide any index, it would remove and return the last item in the list.

On the other hand, if the index that we are providing, is out of range, then we would get an error, saying that the pop index is out of range.

You can use the pop method for the list, as and when required in the Python programs.

Frequently asked questions about the pop method from the list

Q: What is a pop method in Python?

Ans: Using the pop method for list in Python, we can remove the element on the specified position from the list. It returns the removed element.

Q: What if you do not provide an index to the pop method?

Ans: The index argument for the pop method is optional. This means that even if we do not provide the argument, it is fine. In such a situation, it would remove and return the last item in the list.

Q: What if you call the pop method on an empty list?

Ans: If you try to call the pop method for an empty list, you would get an error.