Python offers several convenient mechanisms for working with lists, including multiple ways to remove elements from them. This article explains how the remove()
, pop()
, clear()
, and del
keyword work in Python.
Four Main Ways to Remove Items from a List
We will discuss four main methods for removing items from a list. In some cases, their functionality overlaps, so they can be interchangeable.
remove()
Method: Removing by Value
The remove()
method is used when we know the exact value we want to remove. It takes the value as an argument, finds the first matching element, removes it, and returns nothing:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.remove(1)
print(new_list)
# ['zero', [2.1, 'two and two'], 3, 'IV']
You can also remove more complex objects like nested lists. The key is to pass the object accurately to the method:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.remove([2.1, 'two and two'])
print(new_list)
# ['zero', 1, 3, 'IV']
The remove()
method removes only the first occurrence of the desired element. For example, if the list contains multiple 'zero'
strings, it behaves as follows:
change_list = ['zero', 1, [2.1, 'two and two'], 'zero', 3, 'IV']
change_list.remove('zero')
print(change_list)
# [1, [2.1, 'two and two'], 'zero', 3, 'IV']
If you try to remove a value that isn’t in the list, Python will raise a ValueError
.
pop()
Method: Removing by Index
The pop()
method is useful when the exact location of the element to be removed is known. It takes the index as an argument and returns the removed value:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.pop(2)
# [2.1, 'two and two']
print(new_list)
# ['zero', 1, 3, 'IV']
If you pass a negative value, pop()
will count the index from the end:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.pop(-2)
print(new_list)
# ['zero', 1, [2.1, 'two and two'], 'IV']
If you call pop()
without an argument, the last element will be removed because -1
is the default argument:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.pop()
print(new_list)
# ['zero', 1, [2.1, 'two and two'], 3]
If you try to access a non-existent index in the pop()
method, the interpreter will raise an IndexError
.
clear()
Method: Clearing the List
The clear()
method removes everything from the list, effectively clearing it. It takes no arguments and returns no values:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.clear()
print(new_list)
# []
del
Keyword: Removing by Index or Slice
The del
keyword, like the pop()
method, removes elements from the list by index. It can remove single objects as well as entire slices:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del new_list[2]
print(new_list)
# ['zero', 1, 3, 'IV']
If you pass a slice, the element with the right index will not be removed. In the example below, this is the 'IV'
string:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del new_list[1:4]
print(new_list)
# ['zero', 'IV']
To clear the list, simply pass a full slice [:]
:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del new_list[:]
print(new_list)
# []
You can also use del
with negative indices:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del new_list[-4]
print(new_list)
# ['zero', [2.1, 'two and two'], 3, 'IV']
This works with slices as well:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del new_list[-3:-1]
print(new_list)
# ['zero', 1, 'IV']
If you specify a non-existent index while removing a single element, Python will raise an IndexError
.
How to Remove the First Element of a List in Python
When implementing a stack, for example, it’s often necessary to remove the first element of a list. Let’s take our new_list
and see how to do this.
Using the pop(0)
Method
Call pop()
with the index 0
as an argument:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
new_list.pop(0)
print(new_list)
# [1, [2.1, 'two and two'], 3, 'IV']
Using the del
Keyword
Use del
with the element at index 0
:
new_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del new_list[0]
print(new_list)
# [1, [2.1, 'two and two'], 3, 'IV']
How to Remove Multiple Elements
If the elements to be removed are adjacent, using del
is the most convenient.
Suppose we need to remove 4
, 5
, 6
, and 7
from a sequence of numbers from 0
to 9
. The solution would be:
# First, let's create a list with numbers from 0 to 9.
num_list = list(i for i in range(10))
print(num_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Then, specify a slice of the numbers from 4 to 7.
# Note: the element with index 8 is not deleted.
del num_list[4:8]
print(num_list)
# [0, 1, 2, 3, 8, 9]
How to Remove the Last Element of a List in Python
If we want to get rid of the last element, it is most convenient to use pop()
or del
, as before.
Using the pop()
Method
Since the method takes -1
as the default argument, you can omit passing anything:
num_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
num_list.pop()
print(num_list)
# ['zero', 1, [2.1, 'two and two'], 3]
But if you are a true follower of the Zen of Python (“Explicit is better than implicit”), you can specify -1
—nothing will change.
Using the del
Keyword
To remove the last element using del
, you need to pass -1
:
num_list = ['zero', 1, [2.1, 'two and two'], 3, 'IV']
del num_list[-1]
print(num_list)
# ['zero', 1, [2.1, 'two and two'], 3]
Summary
Python provides four main tools for removing elements from lists:
remove()
: Removes by namepop()
: Removes by index, positive or negativeclear()
: Removes all contents of the listdel
: Allows removing a single element or an entire slice at any index