AppDividend
Latest Code Tutorials

Python Pop Function Example | Python List pop() Method

0

Python List pop() method removes the element at the specified position. It returns the deleted item. The Python 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 Pop Function

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 out of 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.

Python List Pop Example | pop() Method Tutorial

List index in Python starts from 0, not 1. So, if you need to pop 2nd element, you need to pass 1 to the pop() method.

#Pass the negative index in 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, then 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.

Pass the negative index in pop() method in python

If you need to delete or remove the given item from the list, you need to use the list remove() method.

#Pass the index which does not exist in List

Take a 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)

 

Pass the index which does not exist in List

It throws an exception saying IndexError: pop index out of range.

#How to remove element From Python List

See the following code to remove item from 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.

The syntax of Dictionary pop() method is the following.

dictionary.pop(key[, default])

Dictionary pop() method takes two parameters:

  1. key – key which is to be searched for removal.
  2. default – the value which is to be returned when the key is not in the dictionary.

The pop() method returns:

  1. If the key is found – removed/popped element from the dictionary
  2. If the key is not found – the value specified as the second argument (default)
  3. If the key is not found and the default argument is not specified – the KeyError exception is raised.

See the following program to remove 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

Finally, Python Pop Function Example | Python List pop() Method Tutorial is over.

#People Also Ask

Python List append() Method

Python List Copy Example

How To Remove Duplicate Items From Python List

Python List Sort Example

Python List Reverse Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.