What Is Difference Between Del, Remove and Pop on Python Lists?
In python del is a keyword and remove(), pop() are in-built methods. The purpose of these three are same but the behavior is different remove() method delete values or object from the list using value and del and pop() deletes values or object from the list using an index.
The del keyword delete any variable, list of values from a list.
del list_name[index] # To delete single value del list_name # To delete whole list
[1, 2, 2, 3, 4, 5] [1, 2, 2, 3, 4] [2, 2, 3, 4]
The remove() method removes the first matching value from the list.
[1, 2, 2, 3, 4, 5] [1, 2, 3, 4, 5] [1, 2, 3, 4]
The pop() method like del deletes value at a particular index. But pop() method returns deleted value from the list.
[1, 2, 3, 3, 4, 5] [1, 2, 3, 3, 4] [2, 3, 3, 4]
del V/S remove() the V/Spop()
|del is a keyword.||It is a method.||pop() is a method.|
|To delete value it uses the index.||To delete value this method uses the value as a parameter.||This method also uses the index as a parameter to delete.|
|The del keyword doesn’t return any value.||The remove() method doesn’t return any value.||pop() returns deleted value.|
|The del keyword can delete the single value from a list or delete the whole list at a time.||At a time it deletes only one value from the list.||At a time it deletes only one value from the list.|
|It throws index error in case of the index doesn’t exist in the list.||It throws value error in case of value doesn’t exist in the list.||It throws index error in case of an index doesn’t exist in the list.|