Python lists provide three methods to remove an element: del,
remove
and pop.
The main difference between the del
, remove
, and pop
methods are that the del
method removes an item using the index, the remove
method removes the first matching item from the list
, and the pop
method removes an item using its index
and returns the removed value.
This tutorial teaches you the difference between del
, remove
and pop
methods on lists in python in detail and when it is appropriate to use them.
The below table shows the quick differences. Read the tutorial to understand in detail.
Del Statement | Remove Method | Pop Method |
---|---|---|
Deletes item based on index | Deletes item based on Value | Deletes item based on index |
Supports deleting multiple values | Not possible to delete multiple values at once | Not possible to delete multiple values at once |
Throws IndexError when value is not available | Throws ValueError when value is not available | Throws IndexError when value is not available |
Table of Contents
Del Method
The del statement is used to remove the element using the index in Python.
Del statement is used to
- delete a single element from the list using its index.
- slice a list. This means you can use an
index
range to remove multiple elements from the list at once. - Clear the entire list
It throws an IndexError exception when the passed index is out of range of the list indexes. However, while slicing, the slice indexes are silently truncated to be in the allowed range.
Let us see the usage of the del
statement with examples.
Code
The code below demonstrates how to remove an item from the list using its index.
List index is 0
based, and while using the index 0
, the first element is deleted from the list.
colours = ['Orange', 'White', 'Green', 'Blue']
del colours[0]
colours
Output
The item Orange available at the first position is deleted, and the other three items are available in the list.
['White', 'Green', 'Blue']
Using Del Statement to Slice a List
The code below demonstrates how to slice a list
using the del
statement and the range of the index
.
del colours[1:3]
where,del
– statementcolours
– list name1:3
– range of items to be deleted. The first index is inclusive, and the second index is exclusive.
Example
Here, you’ll use the range 1:3
with the del statement. The index is 0
based. Hence, the del
statement deletes the second and third element.
colours = ['Orange', 'White', 'Green', 'Blue']
del colours[1:3]
colours
Output
The second and the third elements are deleted based on indexes 1
and 2
. The other two elements are available in the list.
['Orange', 'Blue']
This is the usage of the del
statement.
Remove Method
The remove() method is used to remove the first matching item from the list.
The remove()
method removes an item using its value instead of the index.
String
values parameter is case-sensitive in the remove()
method.
A valueError exception is thrown when the passed value is unavailable in the list.
Code
The code below demonstrates how to delete an item from the list using the remove()
method.
The value to be deleted is directly passed to the remove()
method.
colours = ['Orange', 'White', 'Green', 'Blue']
colours.remove('Orange')
colours
Output
The value Orange is removed from the list.
['White', 'Green', 'Blue']
Now let us see what happens when a value is not present in the list.
The o is passed in a lower case. But the item in the list is available with the upper case O.
colours = ['Orange', 'White', 'Green', 'Blue']
colours.remove('orange')
colours
Output
Hence you’ll see a ValueError
.
ValueError Traceback (most recent call last)
/var/folders/gb/T/ipykernel_15464/2243630193.py in <module>
1 colours = ['Orange', 'White', 'Green', 'Blue']
2
----> 3 colours.remove('orange')
4
5 colours
ValueError: list.remove(x): x not in list
This is the usage of the remove()
method.
Pop Method
The pop() method removes and returns the item at the specified index of the list.
The pop()
method removes an item using its index instead of the value. After removing the item, it also returns the removed item. Hence, you can use the pop()
method when you want to use the removed item for your operations.
If no index is passed to the pop()
method, the last element of the list is removed.
Code
The code below demonstrates how to remove the first item from the list using the pop()
method.
The index is 0-based. Hence to remove the first item, 0 is passed.
colours = ['Orange', 'White', 'Green', 'Blue']
colours.pop(0)
Output
After removing the item Orange, it returns it. Hence, you’ll see the removed item as output.
'Orange'
This is the usage of the pop()
method.
Pop from End of List
To remove the last item from the list using the pop()
method, you can just invoke the pop()
method without passing any index. Alternatively, you can also pass -1
as a parameter.
To get the last element from the list before removing it, read How To Get The Last Element Of A List In Python.
Code
colours = ['Orange', 'White', 'Green', 'Blue']
colours.pop()
Output
The last item is removed from the list and returned.
'Blue'
Conclusion
Removing an item from the list has multiple options. You’ve learned the difference between del, remove and pop methods to remove an item from the list in Python.
You’ve also learned when it is appropriate to use these different methods.