Difference between remove, del, pop and clear in python list
Introduction
In this article, we will learn different methods to delete items from the python list. So there are 4 methods by which we can delete or remove elements from the python list. which are as follows
- remove
- pop
- clear
- del
Following figure shows brief difference

Now let’s understand one by one
remove()
The remove method removes the first matching element. remove() takes exactly one argument which will be deleted from the list
Example
list = [9, 8, 7, 8, 6, 5] list.remove(8) #This method remove first matching 8 in this list
Output

Note – If the element doesn’t exists in the list, it throws valueError exception, example shown in the following figure

What remove() method returns?
remove() method does return any value or returns None
pop()
The pop method, removes the last value from the list or returns that removed value. Syntax of the pop() method is as follows
Syntax
<list>.pop([index])
- index is optional parameter in pop() method
- If index given then, removes the item at a specific index and returns removed value.
- If index is not available then, the last element is popped out and returns that.
Example
>>> list = [9, 8, 7, 8, 6, 5] >>> list.pop() 5 >>> list [9, 8, 7, 8, 6] >>> list.pop(2) 7 >>> list [9, 8, 8, 6]
Note – If index doesn’t exists then it throws indexError exception, example shown in the following figure

clear()
The clear() method , removes all elements of the list and returns None.
Example
>>> list = [9, 8, 7, 8, 6, 5] >>> list [9, 8, 7, 8, 6, 5] >>> list.clear() >>> list []
del
The del, Remove items by index or slice. This is one of the dangerous method.
There are two different syntax for del method: [] and ().
Example of the delete method is as follows
Example 1: With index
>>> list = [9, 8, 7, 8, 6, 5] >>> del list[2] >>> list [9, 8, 8, 6, 5] >>> del list[-1] >>> list [9, 8, 8, 6]
Example 2: With slice
>>> list = [9, 8, 7, 8, 6, 5] >>> del list[2:] >>> list [9, 8]
>>> list = [9, 8, 7, 8, 6, 5] >>> del list[2:4] >>> list [9, 8, 6, 5]
Example 3: Slice with steps. You can also specify step as [start:stop:step]
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> del list[0:9:2] >>> list [2, 4, 6, 8, 10]
Example 4: With parentheses
>>> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> del(list) >>> list <class 'list'>