Python List pop() method
Python List pop() method
The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list.
Syntax
list.pop([i])
Parameter
i: It is an optional parameter which represents a number specifying the position of the element you want to remove, and by default, its value is -1.
Return
The pop() method returns the item present at the specified index and thus, removing the given item from the list.
Example 1
# Python Program explaining # the list.pop() method fruits = ['apple', 'banana', 'cherry'] print("List: ",fruits) #removing the first element from the list fruits.pop(1) print("After removing the first element ") print("List: ",fruits)
Output
List: ['apple', 'banana', 'cherry'] After removing the first element: List: ['apple', 'cherry']
Example 2
# Python Program explaining # the list.pop() method listVal = [ 11, 12, 13, 14, 15, 16 ] # removeing the last print(listVal.pop(), listVal) # removing the 0th index print(list1.pop(0), list1)
Output
6 [1, 2, 3, 4, 5] 1 [2, 3, 4, 5]