Table of Contents
In this tutorial, we will see about Python List‘s pop method.Python List pop method is used to remove and return the element at specified index.
Python List insert syntax
1 2 3 4 5 |
list1.pop(index) or list1.pop() |
here list1 is object of list.
Python List pop example
You can simply use pop method to remove and return element at given index.If you do not pass anyparameter then it will remove and return last element in the list.
Let’s understand this with the help of simple example.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
listOfVehicles=['Car','Bike','Cycle','Truck'] #Let's remove bike from above list vehicleRemoved=listOfVehicles.pop(1) print("listOfVehicles:",listOfVehicles) print("Removed vehicle:",vehicleRemoved) #Let's use pop method without any argument, it will delete last element by default vehicleRemoved=listOfVehicles.pop() print("listOfVehicles:",listOfVehicles) print("Removed vehicle:",vehicleRemoved) |
Output:
listOfVehicles: [‘Car’, ‘Cycle’, ‘Truck’]
Removed vehicle: Bike
listOfVehicles: [‘Car’, ‘Cycle’] Removed vehicle: Truck
listOfVehicles: [‘Car’, ‘Cycle’] Removed vehicle: Truck
As you can see here, if we do not pass any paramter, then pop method will remove last element from the list.
That’s all about Python List pop method.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.