Difference between del, remove and pop on lists
In this article, we will learn the difference between the del, remove, and pop functions of a list in Python. We will use these built-in functions and discuss the difference with examples. Let's first have a quick look over what is a list in Python.
Python List
Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values. For example,
list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]
List support three different functions to delete elements from the given list. Let us discuss the different effects of the three different methods to remove an element from a list.
Python del
Python list has a keyword del
to remove the elements of a given list. It can remove the element from a specific index, can remove the entire list, and can also perform list slicing. The index is passed as an argument to del. It returns IndexError if the specified index is not present.
list1 = [9,8,7,6]
del list1[1]
print(list1)
[9, 7, 6]
Example: Delete an entire list
It will return an error if you try to print the deleted list.
list1 = [9,8,7,6]
del (list1)
Example: Delete a part of the list
This also allows avoidance of an IndexError if the index is not in the list.
list1 = [9, 8, 7, 6]
del list1[2:]
print(list1)
[9, 8]
Python remove() Function
Python list has a function remove()
to remove the elements of a given list. It removes the first matching value instead of the index, and the value is passed as an argument. It searches the list to find the value and deletes the first matching item it finds. It raises a ValueError if there is no matching value found.
Example: Delete an Element of List
list1 = [0, 2, 3, 2]
list1.remove(2)
print(list1)
[0, 3, 2]
Example: Raises ValueError if not found
list1 = [0, 2, 3, 2]
list1.remove(6)
print(list1)
Traceback (most recent call last):
File "/home/8766bc2d8b0d8fb2b3027df5040c0f1a.py", line 2, in <module>
list1.remove(6)
ValueError: list.remove(x): x not in list
Python pop() Function
Python list has a function pop()
to remove the elements of a given list. It removes the element at a specific index and returns it. It is always preferred to use pop() when you have to delete the last element of the list. It raises an IndexError if the index is out of range.
Example: Deletes specific element
list1 = [4, 3, 5]
#prints the deleted element
print("Deleted element- ", list1.pop(1))
#prints updated list
print("Updated list- ",list1)
Deleted element- 3
Updated list- [4, 5]
Example: Raises IndexError if the index is out of range
list1 = [0, 2, 3, 2]
list1.pop(6)
print(list1)
Traceback (most recent call last):
File "/home/eda51ec0836955c0a07d36bf260b03cc.py", line 2, in <module>
list1.pop(6)
IndexError: pop index out of range
A Brief Comparision among del, remove, and pop in Python List.
del
|
remove()
|
pop()
|
It takes the specified index and removes the value at that index.
|
It takes a value and removes the first occurrence of that value.
|
It takes index (when given, else take last) and removes the value at that index.
|
It limits itself to a simple deletion.
|
It is the only one that searches the item.
|
It is the only one that returns the value.
|
It is a prefix.
|
It is postfix.
|
It is postfix.
|
Syntax- del list[index] or del list
|
Syntax- list.remove(index)
|
Syntax- list.pop(index) or list.pop()
|
It throws an index error when the index does not exist in the list.
|
It throws a value error when the value does not exist in the list.
|
It throws an index error when an index is out of range.
|
del can be used for any class object.
|
remove is bounded to specific classes.
|
pop is bounded to specific classes.
|
del basically works on the index.
|
Remove basically works on the value.
|
pop basically works on the index.
|
In Python, del is a keyword.
|
In Python, remove() is an in-built method.
|
In Python, pop() is an in-built method.
|
Conclusion
In this article, we learned about the differences between the three methods of the list to remove elements such as pop()
, remove()
and del
. We discussed various examples with the help of a code.