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 on how to add an item to a list.
- Add an item to a list in Python [append, extend, insert]
Remove all items: clear[]
You can remove all items from the list with clear[]
.
l = [0, 1, 2]
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 = [0, 10, 20, 30, 40, 50]
print[l.pop[0]]
# 0
print[l]
# [10, 20, 30, 40, 50]
print[l.pop[3]]
# 40
print[l]
# [10, 20, 30, 50]
You can use negative values to specify the position from the end. The index at the end is -1
.
print[l.pop[-2]]
# 30
print[l]
# [10, 20, 50]
If the argument is omitted, the last item is deleted.
print[l.pop[]]
# 50
print[l]
# [10, 20]
Specifying a nonexistent index raises an error.
# print[l.pop[100]]
# IndexError: pop index out of range
Note that pop[0]
, which removes the first item, is an
O[n]
operation and inefficient. See the official wiki for the computational complexity of various operations on list
.
- TimeComplexity - Python Wiki
The deque
type is provided in the standard library collections module to remove the first item with O[1]
. For example, if you want to treat data as a queue [FIFO], it is more efficient to use deque
.
- Deque with collections.deque in Python
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']
l.remove['Alice']
print[l]
# ['Bob', 'Charlie', 'Bob', 'Dave']
If the list contains more than one matching the specified value, only the first is deleted.
l.remove['Bob']
print[l]
# ['Charlie', 'Bob', 'Dave']
To remove multiple items that satisfy the condition at once, use the list comprehensions described below.
Specifying a nonexistent value raises an error.
# l.remove['xxx']
# ValueError: list.remove[x]: x not in list
You can use in
to check if the list contains the item.
- in operator in Python [for list, string, dictionary, etc.]
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 is -1
.
l = [0, 10, 20, 30, 40, 50]
del l[0]
print[l]
# [10, 20, 30, 40, 50]
del l[3]
print[l]
# [10, 20, 30, 50]
del l[-1]
print[l]
# [10, 20, 30]
del l[-2]
print[l]
# [10, 30]
You can delete multiple items with slice.
l = [0, 10, 20, 30, 40, 50]
del l[2:5]
print[l]
# [0, 10, 50]
l = [0, 10, 20, 30, 40, 50]
del l[:3]
print[l]
# [30, 40, 50]
l = [0, 10, 20, 30, 40, 50]
del l[-2:]
print[l]
# [0, 10, 20, 30]
It is also possible to delete all items by specifying the entire range.
l = [0, 10, 20, 30, 40, 50]
del l[:]
print[l]
# []
You can also specify step
as [start:stop:step]
.
l = [0, 10, 20, 30, 40, 50]
del l[::2]
print[l]
# [10, 30, 50]
See the following article for details on slices.
- How to slice a list, string, tuple in Python
Remove items that meet the condition: List comprehensions
Removing items that satisfy the condition is equivalent to extracting items that do not satisfy the condition.
For this purpose, list comprehensions are used.
- 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 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 = [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.
- Extract, replace, convert elements of a list in Python
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']
See the following article for examples for a list of strings.
- Extract and replace elements that meet the conditions of a list of strings in Python
If you want to remove duplicate elements, use set[]
.
- Remove/extract duplicate elements from list in Python
print[list[set[l]]]
# ['Charlie', 'Bob', 'David', 'Alice']