Remove an item from a list in Python (clear, pop, remove, del)
In Python, use list
methods clear()
, pop()
, and remove()
to remove items (elements) from a list. It is also possible to delete items using del
statement by specifying a position or range with an index or slice.
- Remove all items:
clear()
- Remove an item by index and get its value:
pop()
- Remove an item by value:
remove()
- Remove items by index or slice:
del
- Remove items that meet the condition: List comprehensions
See the following article for adding items to the list.
Remove all items: clear()
You can remove all items from the list with clear()
.
l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l.clear()
print(l)
# []
Remove an item by index and get its value: pop()
You can remove the item at the specified position and get its value with pop()
.
The index at the beginning is 0
(zero-based indexing).
l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l.pop(0))
# 0
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l.pop(3))
# 4
print(l)
# [1, 2, 3, 5, 6, 7, 8, 9]
You can use negative values to specify the position from the end. The index at the end is -1
.
print(l.pop(-2))
# 8
print(l)
# [1, 2, 3, 5, 6, 7, 9]
If the argument is omitted, the last item is deleted.
print(l.pop())
# 9
print(l)
# [1, 2, 3, 5, 6, 7]
Specifying a nonexistent index raises an error.
# print(l.pop(100))
# IndexError: pop index out of range
Remove an item by value: remove()
You can remove the first item from the list where its value is equal to the specified value with remove()
.
l = ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']
print(l)
# ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']
l.remove('Alice')
print(l)
# ['Bob', 'Charlie', 'Bob', 'Dave']
If the list contains more than one matching the specified value, only the first one is deleted.
l.remove('Bob')
print(l)
# ['Charlie', 'Bob', 'Dave']
Specifying a nonexistent value raises an error.
# l.remove('xxx')
# ValueError: list.remove(x): x not in list
Remove items by index or slice: del
clear()
, pop()
and remove()
are methods of list
. You can also remove elements from a list with del
statements.
Specify the item to be deleted by index. The first index is 0
, and the last index is -1
.
l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del l[0]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
del l[-1]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8]
del l[6]
print(l)
# [1, 2, 3, 4, 5, 6, 8]
You can delete multiple items with slice.
l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del l[2:5]
print(l)
# [0, 1, 5, 6, 7, 8, 9]
l = list(range(10))
del l[:3]
print(l)
# [3, 4, 5, 6, 7, 8, 9]
l = list(range(10))
del l[4:]
print(l)
# [0, 1, 2, 3]
l = list(range(10))
del l[-3:]
print(l)
# [0, 1, 2, 3, 4, 5, 6]
It is also possible to delete all items by specifying the entire range.
l = list(range(10))
del l[:]
print(l)
# []
You can also specify step
as [start:stop:step]
.
l = list(range(10))
del l[2:8:2]
print(l)
# [0, 1, 3, 5, 7, 8, 9]
l = list(range(10))
del l[::3]
print(l)
# [1, 2, 4, 5, 7, 8]
See the following article for details on slices.
Remove items that meet the condition: List comprehensions
Removing items that satisfy the condition is equivalent extracting items that do not satisfy the condition.
For this purpose, list comprehensions are used.
- Related: List comprehensions in Python
An example of removing odd or even items (= keeping even or odd items) is as follows. %
Is the remainder operator and i % 2
is the remainder of dividing i
by 2
.
In the list comprehension, a new list is generated. Unlike the list
type method or del
statement introduced so far, the original list is not changed.
l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print([i for i in l if i % 2 == 0])
# [0, 2, 4, 6, 8]
print([i for i in l if i % 2 != 0])
# [1, 3, 5, 7, 9]
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
See the following article for details on extracting elements using list comprehensions.
Other examples are as follows.
l = ['Alice', 'Bob', 'Charlie', 'Bob', 'David']
print(l)
# ['Alice', 'Bob', 'Charlie', 'Bob', 'David']
print([s for s in l if s != 'Bob'])
# ['Alice', 'Charlie', 'David']
print([s for s in l if s.endswith('e')])
# ['Alice', 'Charlie']
If you want to remove duplicate elements, use set()
.
print(list(set(l)))
# ['David', 'Alice', 'Charlie', 'Bob']