Removing elements from a list (array) in Python: clear(), pop(), remove(), del

Money and Business

To remove an element from a list (array) of type list in Python, use the list methods clear(), pop() and remove(). You can also specify the position and range of the list by using index or slice and then delete it using the del statement.

The following information is provided here.

  • Remove all elements:clear()
  • Delete an element at the specified position and get its value.:pop()
  • Searches for elements with the specified value and removes the first element.:remove()
  • Deleting by specifying position and range in index slice:del
  • Batch delete multiple elements that meet the criteria.:list inclusion indication

Note that lists can store data of different types, and are strictly different from arrays. Use array (standard library) or NumPy when you want to handle arrays for processes that require memory size or memory address, or for numerical computation of large-scale data.

Remove all elements: clear()

In the list method clear(), all the elements are removed, resulting in an empty list.

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l.clear()
print(l)
# []

Delete an element at the specified position and get its value.: pop()

The method pop() of a list can be used to delete an element at a specified position and get the value of that element.

The first (initial) number is 0.

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]

A negative value can also be used to specify the position from the end (last). The end (last) is -1.

print(l.pop(-2))
# 8

print(l)
# [1, 2, 3, 5, 6, 7, 9]

If the argument is omitted and no position is specified, the element at the end (last) is deleted.

print(l.pop())
# 9

print(l)
# [1, 2, 3, 5, 6, 7]

Specifying a non-existent position will result in an error.

# print(l.pop(100))
# IndexError: pop index out of range

Note that pop(0), which removes the first element, requires the following cost and is not an efficient operation. See the following page on the official wiki for the computational complexity of various operations on lists.
O(n)

O(1)The deque type is provided in the standard library collections module as a type that deletes elements to the top at this cost. For example, if you want to treat data as a queue (FIFO), it is more efficient to use deque.

Searches for elements with the specified value and removes the first element.: remove()

The list method remove() can be used to search for elements with the same value as specified and remove the first element.

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 element that matches the specified value, only the first one will be removed.

l.remove('Bob')
print(l)
# ['Charlie', 'Bob', 'Dave']

If a non-existent value is specified, an error occurs.

# l.remove('xxx')
# ValueError: list.remove(x): x not in list

Deleting by specifying position and range in index slice: del

You can also use the del statement to remove elements from the list.

Specify the element to be deleted by its index. The first (initial) index is 0, and the last (final) 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]

If you specify a range with slices, you can delete multiple elements at once.

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 specify the entire range and delete all elements.

l = list(range(10))
del l[:]
print(l)
# []

[start:stop:step]If you specify the range in the slice in this way and specify incremental step, you can delete multiple jumping elements at once.

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]

For more information about slicing, see the following article.

Batch delete multiple elements that meet the criteria.: list inclusion indication

The process of removing elements that satisfy the conditions is equivalent to the process of leaving (extracting) elements that do not satisfy the conditions. The list comprehension notation is used for this kind of processing.

An example of removing odd or even elements (= leaving even or odd elements) is shown below.
%This is the remainder operator.
i % 2
This is the remainder of i divided by 2.

In the list comprehension notation, a new list is created. Unlike the list type methods introduced so far, the original list remains unchanged.

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]

Other examples. A variety of processing is possible depending on the conditional expression.

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 the set set type.

print(list(set(l)))
# ['David', 'Alice', 'Charlie', 'Bob']
Copied title and URL