Python List pop() Method Tutorial

Python List Pop() Method

This is a detailed tutorial of the Python List pop() method. Learn to remove a particular item from the list using its index and return it using list.pop().

Python List pop() Method

The pop() method of the Python List Object is used to remove any item from the list by using its index. This method also returns the item so removed.


pop() Parameters

As shown in the syntax, the pop() Parameter only takes a single argument i.e. index of the item that you want to remove from the given Python List.

This argument is optional as the default value of the index parameter is set to -1. In other words, if you will not provide any argument to this method, it will automatically assume the argument to be -1 and hence will remove & return the last element of the list because -1 is the index of the last element of the list.

In case, you passed an invalid index or the index that is actually out of the list boundaries, this method will raise IndexError: pop index out of range exception.

pop() Return Value

The element or item so removed using the method list.pop(index) is also returned by this method i.e. the item at the index position will be returned.


The following two examples give you more clarity about the usage of the Python List pop() method.

Example 1. Removing a Python List Item using its Index

In the code written above, we’ve taken two different python lists. Using the pop() method, we’re first removing the item at index 2. The removed item is also returned with this method and hence we’ve printed it to the console.

Similarly, you can also make use of the negative Indexing concept to remove the element from the reference from the end of the list. The index -1 represents the last element of the list, -2 represents the second last element and so on. This way, we have removed the third last element from the list and printed the returned value of the list.pop() method.

Python List Pop() Method Example

Example 2. Removing the Last Element From the List using pop() method

As mentioned earlier, the index parameter of the list.pop() method has the default value set to -1 which represents the index of the last element. Hence, you can always remove the last item of the list by using this method without passing any argument and this is illustrated in the following example.

Python List Pop() Method Example To Remove The Last Item

Example 3. IndexError Simulation in case Index Argument is Invalid

In the following Python code, the last index of the defined list 6. But in the pop method, we’ve passed the index argument as 8.

As no item at index 8 is present in the list, this method throws the IndexError: pop index out of range.

Python List Pop() IndexError Example

Note. You can also remove an item from a Python List by the item itself and not by using its index. This can be done using the Python List remove() method. You simply have to provide the item to be removed itself as the argument to the remove method and the item will be removed from the list.

There’s also another way to delete a particular element from the list i.e. by using the del keyword. For example, if you want to delete the element at the 5th index of the list, you can use the del myList[5] command. This will remove the item at 5th index from the Python list named myList.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *