Python List pop() Function

Python pop() | In this post, we will discuss how to remove items or elements from the list using the pop() method. Python provides built-in function pop() that removes and returns the last value from the list or the given index value. This method takes a single argument (index). The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last element).

The Syntax of the pop() method is:

list_name.pop(index)

Parameters:

The pop() method takes a single argument (index) then, the value at index is popped out and removed from the list.

If not passed any argument (index), then the default index -1 is passed as an argument and the last element is popped out and removed from the list.

Return Value:

The pop() method returns popped element. The last element or the given index element returns from the list.

Exception:

When the argument (index) passed to the method is out of range, it throws IndexError: pop index out of range exception.

pop() Function in Python

We will take the list while declaring the variables then, the Python program removes the given index element from the list. Finally, the new list will be displayed on the screen.

# Python program to remove element from list by index

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed index 3 item from the list
my_list.pop(3)

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Python’, ‘Javascript’]

Note: Index in Python starts from 0, not 1.

Python List pop() last

The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last item).

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed item from the list
my_list.pop()

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Python’, ‘HTML’]

Python pop() IndexError

If the index passed to the method is not in range, then the remove() method is getting IndexError: pop index out of range.

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed index 8 item from the list
my_list.pop(8)

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
Traceback (most recent call last):
File “main.py”, line 10, in
my_list.pop(8)
IndexError: pop index out of range

Also See:- Python List remove()

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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