Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22851

Python for Beginners: Difference Between Pop and Remove in Python

$
0
0

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 the remove() method is that the pop() method uses the index of an element to delete it while the remove() 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 the remove() 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, the remove() method doesn’t return any value.
  • If we invoke the pop() method on an empty list, it will raise an IndexError exception.On the other hand, if we invoke the remove() method on an empty list, it will raise the ValueError 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:

The post Difference Between Pop and Remove in Python appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22851

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>