In various tasks, we need to delete or extract elements from a list. We normally do this using the pop()
method and the remove()
method. In this article, we will discuss the major difference between the working of the pop() method and the remove() method in python.
The pop() Method
The pop()
method is used to extract an element from a given list. When invoked on a list, it accepts the index
of the element as an optional input argument and returns the element at the given index after deleting it from the list as shown below.
myList = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", myList)
element = myList.pop(2)
print("The popped element is:", element)
print("The updated list is:", myList)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The popped element is: 3
The updated list is: [1, 2, 4, 5, 6, 7]
If we do not provide any index as the input argument, it removes the element at the last index and returns the value.
myList = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", myList)
element = myList.pop()
print("The popped element is:", element)
print("The updated list is:", myList)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The popped element is: 7
The updated list is: [1, 2, 3, 4, 5, 6]
The remove() Method
The remove()
method is also used to delete an element from a list. The remove()
method, when invoked on a list, takes the value of the element that needs to be removed as an input argument. After execution, it deletes the first occurrence of the input element from the list. You can observe this in the following example.
myList = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", myList)
myList.remove(3)
print("The removed element is:", 3)
print("The updated list is:", myList)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The removed element is: 3
The updated list is: [1, 2, 4, 5, 6, 7]
The remove()
method doesn’t return any value.
Difference Between Pop and Remove
- The major difference between the
pop()
method and theremove()
method is that thepop()
method uses the index of an element to delete it while theremove()
method takes the value of the element as an input argument to delete the element as we have already seen above. - The
pop()
method can be used without an input argument. On the other hand, if we use theremove()
method without an input argument, the program will run into an error. - The
pop()
method returns the value of the element that is deleted. Whereas, theremove()
method doesn’t return any value. - If we invoke the
pop()
method on an empty list, it will raise anIndexError
exception.On the other hand, if we invoke theremove()
method on an empty list, it will raise theValueError
exception.
Conclusion
In this article, we have discussed the difference between the pop() method and the remove() method for lists in python. To know more about lists, you can read this article on list comprehension in python. You might also like this article on set comprehension in python.
Suggested Readings:
- Developing Chat Application in Python with Source Code
- Polynomial Regression Using sklearn Module in Python
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.