Remove an Item from a List in Python: remove, pop, clear, del

Modified: | Tags: Python, List

In Python, you can remove items (elements) from a list using methods such as remove(), pop(), and clear(). You can also use the del statement to delete items by index or slice.

Additionally, list comprehensions can be used to create a new list that excludes items based on a specific condition.

To learn how to add an item to a list, see the following article:

Remove an item by value using remove()

Use the remove() method to remove the first occurrence of a specified value from a list.

l = ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']

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

If the value appears multiple times, only the first match will be removed.

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

To remove multiple items based on a condition, use list comprehensions as described below.

If the specified value does not exist in the list, a ValueError will be raised.

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

To avoid this error, you can check for the presence of a value using the in operator.

Remove and return an item by index using pop()

The pop() method removes the item at a given index and returns its value. Indexing is zero-based.

l = [0, 10, 20, 30, 40, 50]

popped_item = l.pop(0)
print(popped_item)
# 0

print(l)
# [10, 20, 30, 40, 50]

popped_item = l.pop(3)
print(popped_item)
# 40

print(l)
# [10, 20, 30, 50]

You can use negative indices to count from the end of the list, where -1 refers to the last item.

popped_item = l.pop(-2)
print(popped_item)
# 30

print(l)
# [10, 20, 50]

If no index is provided, pop() removes and returns the last item.

popped_item = l.pop()
print(popped_item)
# 50

print(l)
# [10, 20]

Using an invalid index will raise an IndexError.

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

Note that pop(0) is an O(n) operation and can be inefficient for large lists. For the computational complexity of various operations on lists, see the official Python wiki:

To remove the first item with O(1) complexity, consider using deque from the collections module.

Remove all items using clear()

Use the clear() method to remove all items from a list.

l = [0, 1, 2]

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

Remove items by index or slice using del

In addition to the list methods, you can remove items from a list using the del statement.

Specify the index of the item to be deleted. 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]

Use slice notation to delete multiple items.

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 a step value in the slice as [start:stop:step].

l = [0, 10, 20, 30, 40, 50]
del l[::2]
print(l)
# [10, 30, 50]

See the following article for more details on slice syntax.

Remove items based on a condition from a list using list comprehensions

To remove items that meet a certain condition, you can create a new list that includes only the items that do not meet that condition. This is done using list comprehensions.

Here are examples of filtering even and odd numbers. In Python, i % 2 returns the remainder of i divided by 2, which helps determine if i is even or odd.

List comprehensions create a new list, so the original list remains unchanged. This differs from using methods like remove() or the del statement.

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

evens = [i for i in l if i % 2 == 0]
print(evens)
# [0, 2, 4, 6, 8]

odds = [i for i in l if i % 2 != 0]
print(odds)
# [1, 3, 5, 7, 9]

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

For details on extracting items using list comprehensions, see the following article:

Here are other examples of using list comprehensions:

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']

For string-based examples, see the following article:

To remove duplicates from a list, use set():

print(list(set(l)))
# ['Alice', 'Charlie', 'David', 'Bob']

Related Categories

Related Articles