How to Remove a List Element by Value in Python?

Rate this post

Problem Formulation

Given a Python list and an element (value). How to remove the element (value) from the given list?

Here’s an example of what you want to accomplish:

  • Given:
    • List [1, 2, 99, 4, 99]
    • Element 99
  • Return:
    • List [1, 2, 4, 99]

An alternative would return the list with the element (value) 99 removed in all occurrences:

  • Return:
    • List [1, 2, 4]

We’ll first look at the first problem variant in Method 1 and then examine the second in Method 2.

Method 1: list.remove()

The list.remove(element) method removes the first occurrence of the element from an existing list. It does not, however, remove all occurrences of the element in the list!

Here’s a short example:

>>> lst = [1, 2, 99, 4, 99]
>>> lst.remove(99)
>>> lst
[1, 2, 4, 99]

In the first line of the example, you create the list lst. You then remove the integer element 99 from the list—but only its first occurrence. The result is the list with only four elements [1, 2, 4, 99].

Syntax:

You can call this method on each list object in Python. Here’s the syntax:

list.remove(element)

Arguments:

ArgumentDescription
elementObject you want to remove from the list. Only the first occurrence of the element is removed.

Return value:

The method list.remove(element) has return value None. It operates on an existing list and, therefore, doesn’t return a new list with the removed element

Video:

You can also check out our full blog tutorial on the Python list.remove() method.

Removed Element Does Not Exist

Let’s have a look at another example where this fails next!

To remove an element from the list, use the list.remove(element) method you’ve already seen previously:

>>> lst = ["Alice", 3, "alice", "Ann", 42]
>>> lst.remove("Ann")
>>> lst
['Alice', 3, 'alice', 42]

The method goes from left to right and removes the first occurrence of the element that’s equal to the one to be removed.

If you’re trying to remove element x from the list but x does not exist in the list, Python raises a ValueError:

>>> lst = ['Alice', 'Bob', 'Ann']
>>> lst.remove('Frank')
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    lst.remove('Frank')
ValueError: list.remove(x): x not in list

To fix this, you can enclose it in a try/except environment like so:

lst = ['Alice', 'Bob', 'Ann']
try:
    lst.remove('Frank')
except:
    pass

print('done!')

This code goes through and returns 'done!' without any error message.

Method 2: Python List remove() All

The list.remove(x) method only removes the first occurrence of element x from the list.

But what if you want to remove all occurrences of element x from a given list?

The answer is to use a simple loop:

lst = ['Ann', 'Ann', 'Ann', 'Alice', 'Ann', 'Bob']
x = 'Ann'

while x in lst:
    lst.remove(x)

print(lst)
# ['Alice', 'Bob']

You simply call the remove() method again and again until element x is not in the list anymore.

For comprehensibility, I want to show you one method that is not optimal—but that’s recommended all over the web (e.g., here).

Method 3: List Comprehension: Remove Elements Conditionally

Using list comprehension is cheating because this method does not really remove elements from a list object. It merely creates a new list with some elements that meet your condition.


List comprehension is a compact way of creating lists. The simple formula is:

[ expression + context ]

  • Expression: What to do with each list element?
  • Context: What list elements to select? It consists of an arbitrary number of for and if statements.

The example [x for x in range(3)] creates the list [0, 1, 2].


You can also define a condition such as “all values unlike a given element 1”, e.g., x!=1, in the context part by using an if condition.

This leads us to a way to remove all elements that are equal to a certain value such as our value 99 in a given list.

>>> lst = [1, 2, 99, 4, 99]
>>> lst_new = [x for x in lst if x != 99]
>>> lst_new
[1, 2, 4]

While you iterate over the whole list lst, you include only the elements that are not equal to the element 99. Thus, the resulting list has all elements 99 removed.

However, the original list lst has not changed!

>>> lst
[1, 2, 99, 4, 99]

Related blog articles:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!