In this Python post, we’ll have a detailed practical discussing on how to use Python list pop. We’ll make use of multiple examples to practically demonstrate how pop works in Python list.
I am sure that after reading this detailed guide, you’ll easily be able to incorporate Python list pop in your own code.
So without any further delay, let’s start understanding pop method in detail.
- What is Python List Pop?
- Syntax of Pop Method
- Implementing Python List Pop Method (Multiple Examples)
- Example 1: Remove Specific Item From List
- Example 2: Pop with No Specific Index
- Example 3: Store the Popped Value
- Example 4 : Pop Using Negative Indices
What is Python List Pop?
The pop method is used to remove items from the list of Python. You can specify the index of that specific item you want to remove and pass it to the pop method.
This method will remove the item from list and will store that item in case you want to use it somewhere else.
If you don’t pass any specific index to this method, then it will automatically remove the last item from list.
Let’s now implement Python list pop method using examples to see this theory in practical.
Syntax of Pop Method
list.pop(pass an index) //remove specific index item of list list.pop() // remove last item of list
As you can see here, it is a method of Python list and by passing a specific index will make to remove that item which is present on that index.
Just make sure that the index passed to that method should be within the range of list, or else it’ll throw an out of range index error.
Implementing Python List Pop Method (Multiple Examples)
See the below examples for complete demonstration.
Example 1: Remove Specific Item From List
For that, we’ll first initialize a list with some items and then use the pop method on them. See below code:
listOfItems=[2,3,4,6,7] print(listOfItems) // [2,3,4,6,7] listOfItems.pop(2) // will pop the 3rd item as the index starts from 0 so 0,1,2 print(listOfItems) // [2,3,6,7]
You can see that the third item from the list is successfully removed.
Example 2: Pop with No Specific Index
Let’s now see what happens if we don’t pass an index to the Python list pop method of list.
listOfItems=[2,3,4,6,7] print(listOfItems) // [2,3,4,6,7] listOfItems.pop() // removes last item of list print(listOfItems) // [2,3,4,6,7]
Example 3: Store the Popped Value
listOfItems=[2,3,4,6,7] poppedItem = listOfItems.pop(3) // popped item is stored in the variable print(poppedItem) // 6
You can see that the pop method stores the popped item data. We have passed it to a variable and the data is successfully printed.
Example 4 : Pop Using Negative Indices
It is used if you want to pop item that starts from the right/end. See below code:
listOfItems=[2,3,4,6,7] poppedItem = listOfItems.pop(-1) // -1 specifies the last item of list print(poppedItem) // 7
As you can see here, the last item is popped. Similarly, you can use -2 to pop the second last item.
Basically, the difference between the positive and negative indices is that positive starts from 0 to length of list -1 (0,1,2,….list length -1) while negative indices starts from 0 to the length of list (-1,-2,-3,…..length of list).
So this is how we can easily use Python list pop method. I’ve tried to explain the usage of pop method as much as I can to give you a proper understanding.
Feel free to ask questions regarding the implementation of Python list pop, if you still have any. I’d love to answer all.
As a conclusion, hope you now have a proper idea of how to properly use Python list pop in your own Python code. I’d be looking forward at your valuable feedback on this Python post. Thanks you for reading this post.
You may like to read: