Python list remove
last modified November 16, 2026 Python 3.15
In this article we show how to remove list elements in Python.
A list is an ordered, mutable collection of values. Its elements
are accessed by zero-based indexes. Python provides several ways to delete
elements: the remove, pop, and clear
methods, and the del statement. To remove elements that satisfy
a condition, we usually build a new list with a comprehension.
The methods differ in what they take and what they return:
remove deletes by value, pop deletes by index and
returns the element, del deletes by index or slice, and
clear empties the whole list.
Removing by value with remove
The remove method deletes the first occurrence of the given
value. It raises ValueError if the value is not present.
words = ['sky', 'cup', 'new', 'war', 'wrong', 'crypto', 'forest',
'water', 'cup']
words.remove('cup')
print(words)
words.remove('cup')
print(words)
try:
words.remove('cup')
except ValueError as e:
print(f'error: {e}')
The list contains the word cup twice. Each call to
remove deletes only the first occurrence, so we need two calls to
get rid of both. The third call fails because no cup is left.
$ python remove_value.py ['sky', 'new', 'war', 'wrong', 'crypto', 'forest', 'water', 'cup'] ['sky', 'new', 'war', 'wrong', 'crypto', 'forest', 'water'] error: list.remove(x): x not in list
Checking membership before removing
To avoid the ValueError, we can check with the in
operator whether the value is in the list.
words = ['sky', 'cup', 'new', 'war']
for word in ['cup', 'pen']:
if word in words:
words.remove(word)
print(f'{word} removed')
else:
print(f'{word} not in list')
print(words)
We try to remove two words; only one of them is in the list. The membership
test scans the list, as does remove, so for frequently repeated
removals from large collections a set is a better choice.
$ python remove_safe.py cup removed pen not in list ['sky', 'new', 'war']
Removing by index with pop
The pop method removes the element at the given index and returns
it. Without an argument, it removes the last element. It raises
IndexError if the list is empty or the index is out of range.
words = ['sky', 'cup', 'new', 'war', 'wrong', 'crypto', 'forest',
'water', 'cup']
w = words.pop(0)
print(f'{w} has been deleted')
w = words.pop()
print(f'{w} has been deleted')
w = words.pop(-2)
print(f'{w} has been deleted')
print(words)
try:
[].pop()
except IndexError as e:
print(f'error: {e}')
We remove the first element, the last element, and the last but one element,
which is addressed by the negative index -2. Since
pop returns the removed element, we can print it.
Popping from the end of a list is fast; popping from the beginning shifts all
remaining elements. For queues that remove from the front, use
collections.deque and its popleft method.
$ python pop_elements.py sky has been deleted cup has been deleted forest has been deleted ['cup', 'new', 'war', 'wrong', 'crypto', 'water'] error: pop from empty list
Deleting with del
The del statement deletes an element at the given index, or a
whole slice of elements. Unlike pop, it does not return anything.
words = ['sky', 'cup', 'new', 'war', 'wrong', 'crypto', 'forest',
'water', 'cup']
del words[0]
del words[-1]
print(words)
vals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del vals[0:4]
print(vals)
del vals[::2]
print(vals)
We delete the first and the last element of the words list.
del vals[0:4]
Here we delete the elements with indexes 0 to 3; the end index of a slice is not included.
del vals[::2]
A slice with a step deletes every second element of the remaining list.
$ python del_statement.py ['cup', 'new', 'war', 'wrong', 'crypto', 'forest', 'water'] [4, 5, 6, 7, 8, 9, 10] [5, 7, 9]
Clearing a list
The clear method deletes all elements of a list. It modifies the
list object in place, which matters when other variables refer to the same
list.
words = ['sky', 'cup', 'new', 'war', 'wrong']
alias = words
print(f'there are {len(words)} words in the list')
words.clear()
print(f'there are {len(words)} words in the list')
print(f'alias: {alias}')
vals = [1, 2, 3]
other = vals
vals = []
print(f'vals: {vals}, other: {other}')
After words.clear, the alias variable shows an
empty list as well, because both names refer to one object. The
del words[:] statement has the same effect.
vals = []
Assigning an empty list is different: it binds the name vals to
a new list and leaves the original list, still referenced by
other, untouched.
$ python clear_list.py there are 5 words in the list there are 0 words in the list alias: [] vals: [], other: [1, 2, 3]
Removing all occurrences of a value
The remove method deletes only the first match. To delete all
occurrences, we can build a new list without them, or call
remove repeatedly.
vals = [1, 2, 3, 2, 4, 2, 5]
cleaned = [e for e in vals if e != 2]
print(cleaned)
while 2 in vals:
vals.remove(2)
print(vals)
The comprehension keeps all elements that are not equal to 2. The
while loop calls remove as long as the value is in
the list. Both give the same result, but the loop scans the list again on
every check and removal, so the comprehension is preferable for long lists.
$ python remove_all.py [1, 3, 4, 5] [1, 3, 4, 5]
Removing elements by condition
To remove all elements that satisfy a condition, we keep the elements that do
not satisfy it. A list comprehension or the filter function does
this in one step.
words = ['sky', 'cup', 'new', 'war', 'wrong', 'crypto', 'forest',
'water', 'cup']
alias = words
short = [w for w in words if len(w) > 3]
print(short)
words[:] = [w for w in words if not w.startswith('w')]
print(words)
print(alias is words, alias)
vals = [3, -1, 0, 7, -4, 2, 0]
positive = list(filter(lambda e: e > 0, vals))
print(positive)
The first comprehension creates a new list without the words that have three or fewer characters.
words[:] = [w for w in words if not w.startswith('w')]
Assigning to the full slice words[:] replaces the contents of the
existing list instead of creating a new one. The alias variable
still refers to the same, now filtered, list.
positive = list(filter(lambda e: e > 0, vals))
The filter function returns an iterator of the elements for which
the function returns true; we turn it into a list.
$ python remove_condition.py ['wrong', 'crypto', 'forest', 'water'] ['sky', 'cup', 'new', 'crypto', 'forest', 'cup'] True ['sky', 'cup', 'new', 'crypto', 'forest', 'cup'] [3, 7, 2]
Deleting by index inside a loop
Deleting elements while looping forward over a list skips elements, because the remaining elements shift to the left. Looping over the indexes backwards avoids the problem: a deletion only shifts elements we have already visited.
vals = [10, 11, 12, 13, 14, 15]
for i in reversed(range(len(vals))):
if vals[i] % 2 == 1:
del vals[i]
print(vals)
We delete the odd numbers in place. The reversed(range(len(vals)))
expression yields the indexes from the last to the first, so every deletion
leaves the indexes of the elements still to be checked unchanged.
$ python remove_in_loop.py [10, 12, 14]
Removing duplicates
Duplicates are removed by converting the list to a collection that stores each value only once.
words = ['sky', 'cup', 'sky', 'war', 'cup', 'water', 'war', 'sky'] unique = list(dict.fromkeys(words)) print(unique) print(sorted(set(words)))
Dictionary keys are unique and dictionaries preserve insertion order, so
dict.fromkeys removes duplicates and keeps the first occurrence
of each word in its original position. A set also removes
duplicates, but it has no order; here we sort its elements.
$ python remove_duplicates.py ['sky', 'cup', 'war', 'water'] ['cup', 'sky', 'war', 'water']
Source
More on lists – Python tutorial
Mutable sequence types – Python documentation
In this article we have shown how to remove list elements in Python. See also Python add list element, Python filter list, and Python list.
Author
List all Python tutorials.