How Can We Help?
Introduction:
In this tutorial, we are going to explain how to use Python list pop() Method with basic syntax and many examples for better understanding.
Python list pop() method It removes last object from the list and returns detail of removed object of a list.
Syntax:
<list_variable_name>.pop(obj = list[-1])
Input Parameters:
- object : is an index of the object ,which to be removed from the list. Object is an optional parameter.
- list_variable_name : The lists that we want to count a particular element inside.
Examples
Python list pop first example
alphabet=['a', 'b', 'c', 'd'] print("popped element: ", alphabet.pop(0))
Output of example
Popped element: a
In the above example, after applying pop operation as ” alphabet.pop(0)”, list variable ‘alphabet’ returned the first element in the list that has been deleted from the list.
Python list pop last example
alphabet=['a', 'b', 'c', 'd'] print("popped element: ", alphabet.pop())
Output of example
Popped element: d
In the above example, after applying pop operation as ” alphabet.pop(0)”, list variable ‘alphabet’ returned the first element in the list that has been deleted.
Python list pop by index example
alphabet=['a', 'b', 'c', 'd'] print("popped element: ", alphabet.pop(2))
Output of example
Popped element: c
In the above example, after applying pop operation as ” alphabet.pop(2)”, list variable ‘alphabet’ returned the third element “c” in the list that has been deleted.
Python list pop by value example
alphabet=['a', 'b', 'c', 'd'] print("popped element: ", alphabet.pop(alphabet.index('a')))
Output of example
Popped element: a
In the above example, here we have a little-bit complex that we have used list.index(‘a’) method to get the index of “a” character to be able to delete this particular value from the list, then the list variable ‘alphabet’ returned the element “a” in the list that has been deleted.
In this tutorial, you have learned how to delete an element from list using list.pop() method. we have demonstrated that with different examples like delete first or last element from list or delete particular value from list.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
Similar Python list Methods:
- list.append(object) : It appends object to the given list.
- list.count(object) : It returns count of how many times object occurs in the list.
- list.extend(seq) : It appends object to the given list.
- list.insert(index,object) : It inserts object into the list at offset index.
- list.pop(obj=list[-1]) : It removes last object from the list and returns removed object of a list.
- list.remove(object) : It removes object from the list.
- list.reverse() : It reverses objects of list in place.
- list.sort([func]) : It sorts objects of a list and use compare func, if given.
Python tutorials list:
- Python tutorials list : access all python tutorials.