Menu Close

Python List pop() Method

python list pop() method

In this tutorial, we are going to learn the Python list pop() method. In Python pop() removes the element on the given index and returns the removed element.
In the previous tutorial, we have seen the list reverse() method to reverse the items of the list and the list insert() method to insert items on a specific index number.

Python list pop() method:

In Python, pop() is a built-in list method, which is used to delete the item on a given index number and return the deleted item from the list.

list pop() method syntax:

Syntax of the Python list pop method is:

list.pop(index)

list pop() method parameter:

In the list pop function take a single parameter index.
index:- index represents the index number of the list item.

If the index number is not passed into the list pop function, the list pop method takes default -1 ( Index of the last item ) as a parameter.

Return value of list pop() method.

The list pop function returns the item present in the list at the given index. This item is also removed from the list.

List pop() method Examples:

In the below example, we will delete the item present on index number 3.

Example 1:

#In this example we passed 3 index number as a parameter.
lang = ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
print('Original List:- ', lang)
deleted_item = lang.pop(3)
print('Deleted Item:- ', deleted_item)

Output

Original List:-  ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
Updated List:-  C++

Example 2:

#In this example we have not pass any parameter but still it 
#remove the last item from the list.
lang = ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
print('Original List:- ', lang)
deleted_item = lang.pop()
print('Deleted Item:- ', deleted_item)

Output

Original List:-  ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
Deleted Item:-  HTML

Note:- if you do not pass any index number in the list pop() method, it will delete the last item from the list.

Conclusion:

In this tutorial, you have learned the Python list pop() method to remove the item from the list. list pop function takes the index number as a parameter and deletes the item that presents on the index number and
return the deleted item.

I hope this Python pop() function tutorial will help you If you like this article please share it with your friends who want to become Python developers.

Other Useful Python List Methods:

For More Information:- Click Here

Python List insert() Method
Python List count() Method

Related Posts