Difference between remove, del and pop in Python list
Let’s know the basics of removing items in a list.
First of all, let’s discuss how a list looks like.
Moreover, it will be helpful because those methods are based on lists. Here in this article, I am going to tell you the difference between remove, del and pop which is related to Python list.
Remove vs del vs pop in Python
See the code below:
list_gvj = [ 1, 2, 12, 45, 789, "raj" ] print ( list_gvj[3] ) # accessing elements by index print (list_gvj) # prints whole list
The output of the code is given below:
45 [1, 2, 12, 45, 789, 'raj']
Certainly, there are different ways to deal with elements in the list.
- remove: This method is used to remove the elements in the list by value.
- del: This method allows to delete the elements using its index. It doesn’t return any value.
- pop: This method is used to delete the element by index and returns the deleted value.
It seems very confusing because it’s just their definitions.
remove() in a List :
As discussed earlier, basically, we are going to remove an element in a list.
Syntax: list. remove (element)
list_gvj = [ 1, 2, 12, 45, 789, "raj" ] print ("Initial list :",list_gvj) # prints the above list list_gvj.remove(12) # remove(element) removes the element from the list print ("Final list :",list_gvj)
Output : Initial list : [1, 2, 12, 45, 789, 'raj'] Final list : [1, 2, 45, 789, 'raj']
Certainly, what if the element is not present in the list?
list_gvj = [ 1, 2, 12, 45, 789, "raj" ] list_gvj.remove(1234) # remove(element) removes the element from the list # 1234 is not there in the list print ("Final list :",list_gvj)
Output : Traceback (most recent call last): File "main.py", line 5, in <module> list_gvj.remove(1234) # remove(element) removes the element from the list ValueError: list.remove(x): x not in list
del( ) in a List :
del( ) function is used to delete the element in the list by its index.
Let’s see with an example of how it’s different from the remove method.
list_gvj = [ 1, 2, 12, 45, 789, "raj" ] print ("Initial list :",list_gvj) # prints the above list del(list_gvj[5]) # del ( list_gvj [ index ] ) removes the element at that index print ("Final list :",list_gvj)
Output : Initial list : [1, 2, 12, 45, 789, 'raj'] Final list : [1, 2, 12, 45, 789]
Certainly, what if the element is not present in the list?
Therefore giving an index -> 34 which is out of range.
list_gvj = [ 1, 2, 12, 45, 789, "raj" ] del(list_gvj[34]) # del ( list_gvj [ index ] ) removes the element at that index # index 34 is out of range so raises error. print ("Final list :",list_gvj)
Output: Traceback (most recent call last): File "main.py", line 3, in <module> del (list_gvj[34]) # del ( list_gvj [ index ] ) removes the element at that index IndexError: list assignment index out of range
Also, read:
pop() in a List :
pop( ) method is same as del( ).
The main difference is,
- del( ) does not return any value.
- pop( ) returns the deleted element in the list.
Syntax: list. pop( index )
list_gvj = [ 1, 2, 12, 45, 789, "raj" ] print ("initial list :",list_gvj) del_value = list_gvj.pop(5) # index 5 is given print ("Deleted value is : ",del_value) print ("Final list :",list_gvj)
Output : initial list : [1, 2, 12, 45, 789, 'raj'] Deleted value is : raj Final list : [1, 2, 12, 45, 789]
Note: list.pop( ) deletes the last element in the list
Concluding the topic, these are the different ways to remove or delete elements in the list. So I hope, you have understood what is the differences between Python remove, del and pop methods.
Leave a Reply