Python List pop() Method With Examples

Introduction

Today we’ll be going the Python list pop() method. We generally have various built-in methods to remove or delete any element from a list in Python. We have del, remove(), as well as the pop() method to accomplish the task. But each one of them has their own differences. Let’s find out how to use the pop() method and what are the benefits of using this method.

Working of Python List pop() Method

Basically, the pop() method in Python pops out the last item in a list when no parameter is passed. When passed with some index, the method pops the element corresponding to the index.

Syntax,

  • When an index is passed, the method removes the element at the index, as well as returns the same,
  • When nothing is passed, the method removes the last element and returns it where the function was previously called.

Using the Python List pop()

Take a look at the example code below, it illustrates the use of the built-in pop() method in python.

Output:

List pop() In Python

List pop() In Python
  • At first, we initialize a list, list1 as [0,1,2,3,4,5,6,7,8,9,10]. On this list, we perform the corresponding pop operation by passing distinct indices
  • pop() – As stated earlier, by default pop() returns and removes the last element from a list. In our case, the last element was 10, which gets popped consecutively
  • pop(0) – This pops the element in the list1, at the 0th position, which is 0 in our case
  • Similarly, all the operations pop(1), pop(2), pop(3), and pop(4) return the items at their respective indices. Which are 2 4 6 and 8 as we continue to pop elements out of the list.

Errors while using Python List pop() Method

1. IndexError with Python pop()

While using the Python list pop() method, we encounter an IndexError if the index passed to the method is greater than the list length.

This Error occurs basically when the index provided it out of the list’s range. Let us look at a small example of this:

Output:

In this example, it is clear that the index provided to the pop() method, 10 is larger than the list’s length(4). Hence, we get the IndexError.

2.Error when the list is empty

Similar to the previous section, when we try to perform the Python List pop() method on an empty list, we face the same IndexError. For example:

Output:

So, we can conclude that while performing Python list pop() method on an empty list, an IndexError is thrown.

Hence, we must check before we apply the pop() method, that the list we are dealing with is not empty. A simple length check can solve our problem.

Output:

See, it’s easy. The if-else statement in Python checks whether the list is empty or not and only pops an element from the list when len(l1) > 0 i.e. when the list l1 is not empty.

Python List pop() on a Python Stack

As we have seen in our Python Stack Tutorial, pop() is also a stack operation used to remove the last task or element pushed. Let us see how we can implement the Python list pop() method in a stack using lists.

Output:

Python List pop() Method

Pop On Stack
  • After declaring a stack list, we push 5 elements by continuously pushing tasks(elements) using the append() method.
  • As soon as our stack initialization is done, we repetitively pop() elements until the stack is empty.
  • Notice that while poping elements from the stack we have used the condition len(stack) > 0 using the while loop. This ensures that the pop operation is performed only while the stack is not empty.

Conclusion

In this tutorial, we learned how the built-in pop() method in python works, errors related to it, as well as its applications in a stack. Feel free to ask any questions about the topic in the comments.

References

By admin

Leave a Reply

%d bloggers like this: