Methods and Functions

Python List pop()

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, python pop, python list pop

Pop Python Syntax

list.pop(index)

pop() parameters

  1. The python pop method takes one parameter as the input from the user. This is the index parameter. 
  2. 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). 
  3. 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

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.