The python pop() method is a built-in function provided by the python compiler used to remove and return the value from the list of a particular item at a given index.
Pop Python Syntax
list.pop(index) |
pop() parameters
- The python pop method takes one parameter as the input from the user. This is the index parameter.Â
- The index parameter is optional. If the parameter is not passed by the user, the default parameter will be -1 (Parameter of the last item).Â
- If the index given as input by the user is not in the range then an IndexError: pop index out of range exception is thrown.
Return Value from pop python
The item at the given index is returned by the pop() function. This item is removed from the list as well.
Example 1: Pop item at the given  index from the list
In the program given below the user has provided a particular index from where the item is to be removed. After using the python pop method the item given in the list is removed and the value is returned.Â
lang = ['Python', 'Java', 'C++', 'French', 'C', 'Angular']
# remove the 4th item and return the value
returnvalue1 = lang.pop(3)
print('Return Value:', returnvalue1)
# Updated List of lang
print('Updated List of Lang:', lang)
Output
Return Value: French
Updated List of Lang: ['Python', 'Java', 'C++', 'C', 'Angular']
Example 2: python pop without an index, and for negative indices
In the program given below, we have removed elements without passing an index and by using negative indices.Â
lang = ['Python', 'Java', 'C++', 'Ruby', 'C']
# remove the last item and return the value
print('When the index is not given by the user:')Â
print('Return Value:', lang.pop())
print('The Updated List of lang:', lang)
# remove the last item and return the value
print('\nWhen -1 is passed as index:')Â
print('Return Value:', lang.pop(-1))
print('The Updated List of lang:', lang)
print('\nWhen -3 is passed as the index:')Â
print('Return Value:', lang.pop(-3))
print('The Updated List of lang:', lang)
Output
When the index is not given by the user:
Return Value: C
The Updated List of lang: ['Python', 'Java', 'C++', 'Ruby']
When -1 is passed as index:
Return Value: Ruby
The Updated List of lang: ['Python', 'Java', 'C++']
When -3 is passed as the index:
Return Value: Python
The Updated List of lang: ['Java', 'C++']
FAQs on Pop Python
Q1. What is pop in Python?
The python pop is an in-built function that removes the element present at a particular index and returns its value to the user.
Q2. What does pop () do?
The pop() function is used to remove the element at the specified index. Look at the example below:
list1111 = [ 1, 2, 3, 4, 5, 6 ]
Â
print(list1111.pop())
Â
# Updated list of elements
print("Updated list: ", list1111)
Â
l2 = [1, 2, 3, ('catto', 'bats'), 4]
Â
# Remove the last three elements from l2
print(l2.pop())
print(l2.pop())
print(l2.pop())
Â
# Print the updated list
print("Updated list: ", l2)
Output:Â
6
Updated list: [1, 2, 3, 4, 5]Â
4
('catto', 'bats')
3
Updated list: [1, 2]
Q3. Can you pop from a python dictionary pop
Yes, the pop() can be used to pop items from a dictionary. Look at the example given below:
phone = {
  "brand": "Apple",
  "model": "12 pro",
  "year": 2020
}
a=phone.pop("Model")
print(phone)
Output:
12 pro
Q4. How do I use pop in Python 3?
In python 3, users can use the pop() to remove the last value or any other specified value from a list.Â
Example:
primenumb11 = [2, 3, 5, 7]
remove_ele1 = primenumb11.pop(2)
print('Removed Element is:', removed_ele1)
Output:
Removed Element is: 5
Leave a Reply