Python list pop() method removes and returns the object at the given index (passed as an argument) from a list. Python pop() is an inbuilt function in Python that removes and returns the last value from the list or the given index value.
Python list pop
Python list pop() is a built-in function that removes the element at the specified position. The pop() function returns the deleted element. The pop() method takes the single argument as a position. In our case, it is an index and removes the item present at that position from the list.
If the index passed to a pop() method is not in the range or does not exist in the list, then it throws IndexError: pop index out of range exception.
The parameter passed to the pop() method is optional. If no parameter is passed, the default index -1 is given as an argument that returns the last element.
The syntax for the python pop() method is the following.
list.pop(index)
The index parameter is required, and it is the position where we need to remove the element from the list.
How does Python pop() method work?
Python pop function takes an index value and checks whether the list exists, removes the item at an index, and returns the item after the removal.
It does not work when the index is out of bounds or range. IndexError gets displayed for out-of-bound values.
The index can also be zero or have positive or negative values. In the case of negative input, the items get accessed in the reverse direction.
Let us take a simple example.
# app.py GoT = ['Daenerys', 'Jon', 'Tyrion'] removedItem = GoT.pop(1) print (removedItem)
The output is the following.
List index in Python starts from 0, not 1. So, if you need to pop the 2nd element, you must pass 1 to the pop() method.
Passing the negative index in the pop() method
Let’s see the following scenario where we pass the negative index to the pop() method.
# app.py GoT1 = ['Daenerys', 'Jon', 'Tyrion'] removedItem1 = GoT1.pop(-1) print (removedItem1)
In python programming, if we pass the negative index, it will start from the end of the list. So in the above example, -1 means the last element of the list, which is Tyrion. So that element will be removed.
If you need to delete or remove the given item from the list, use the list remove() method.
Passing the index which does not exist in the List
Look at the following scenario where the index does not exist in the list and see the output.
# app.py GoT2 = ['Daenerys', 'Jon', 'Tyrion'] removedItem2 = GoT2.pop(4) print (removedItem2)
Output
It throws an exception saying IndexError: pop index out of range.
#How to remove element From Python List
See the following code to remove the item from the list in Python.
#app.py nestedList = [[11, 21], [19, 21], [21, 46], [11, 29], [19, 46]] print("Original List : ", nestedList) nestedList.pop(1) print("Remaining List Items are : ", nestedList) nestedList.pop(3) print("Again Remaining List Items are : ", nestedList)
See the output.
➜ pyt python3 app.py Original List : [[11, 21], [19, 21], [21, 46], [11, 29], [19, 46]] List Items are : [[11, 21], [21, 46], [11, 29], [19, 46]] List Items are : [[11, 21], [21, 46], [11, 29]] ➜ pyt
We have seen almost all the possible scenarios of the list pop() method in Python.
Python Dictionary pop()
The pop() method removes and returns an element from a dictionary having the given key.
Syntax
The syntax of the Dictionary pop() method is the following.
dictionary.pop(key[, default])
Parameters
The dictionary pop() method takes two parameters:
- key – key which is to be searched for removal.
- default – the value to be returned when the key is not in the dictionary.
Return Value
The pop() method returns:
- If the key is found – remove/popped element from the dictionary
- If the key is not found – the value specified as the second argument (default)
- If the key is not found and the default argument is not specified – the KeyError exception is raised.
See the following program to remove an element from Python Dictionary.
#app.py nestedDict = { 'eleven': 'Millie Bobby Brown', 'mike': 'Finn Wolfhard', 'dustin': 'Gaten Matarazzo' } print("Original List : ", nestedDict) nestedDict.pop('mike') print("Modified List : ", nestedDict)
See the output.
➜ pyt python3 app.py Original List : {'eleven': 'Millie Bobby Brown', 'mike': 'Finn Wolfhard', 'dustin': 'Gaten Matarazzo'} Modified List : {'eleven': 'Millie Bobby Brown', 'dustin': 'Gaten Matarazzo'} ➜ pyt
That’s it for this tutorial.