Menu Close

Python List pop() Method

python list pop() method

Python List pop() Method

In this tutorial, we are going to learn the python list pop() method. In Python pop() remove the element on the given index and return 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 list pop method is:

list.pop(index)

list pop() method parameter:

In list pop function take a single parameter index.
index:- index represent 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 below example we will delete the item present on the 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 does not pass any index number in list pop() method, it will delete 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 take index number as a parameter and delete 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 with your friends who want to become Python developer.

For More Information:- Click Here

Previous Article

Was this post helpful?

Related Posts

Leave a Reply