1) Almost-English style:

Test for presence using the in operator, then apply the remove method.

if thing in some_list: some_list.remove(thing)

The removemethod will remove only the first occurrence of thing, in order to remove all occurrences you can use while instead of if.

while thing in some_list: some_list.remove(thing)    
  • Simple enough, probably my choice.for small lists (can't resist one-liners)

2) Duck-typed, EAFP style:

This shoot-first-ask-questions-last attitude is common in Python. Instead of testing in advance if the object is suitable, just carry out the operation and catch relevant Exceptions:

try:
    some_list.remove(thing)
except ValueError:
    pass # or scream: thing not in some_list!
except AttributeError:
    call_security("some_list not quacking like a list!")

Off course the second except clause in the example above is not only of questionable humor but totally unnecessary (the point was to illustrate duck-typing for people not familiar with the concept).

If you expect multiple occurrences of thing:

while True:
    try:
        some_list.remove(thing)
    except ValueError:
        break
  • a little verbose for this specific use case, but very idiomatic in Python.
  • this performs better than #1
  • PEP 463 proposed a shorter syntax for try/except simple usage that would be handy here, but it was not approved.

However, with contextlib's suppress() contextmanager (introduced in python 3.4) the above code can be simplified to this:

with suppress(ValueError, AttributeError):
    some_list.remove(thing)

Again, if you expect multiple occurrences of thing:

with suppress(ValueError):
    while True:
        some_list.remove(thing)

3) Functional style:

Around 1993, Python got lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed them and submitted working patches*. You can use filter to remove elements from the list:

is_not_thing = lambda x: x is not thing
cleaned_list = filter(is_not_thing, some_list)

There is a shortcut that may be useful for your case: if you want to filter out empty items (in fact items where bool(item) == False, like None, zero, empty strings or other empty collections), you can pass None as the first argument:

cleaned_list = filter(None, some_list)
  • [update]: in Python 2.x, filter(function, iterable) used to be equivalent to [item for item in iterable if function(item)] (or [item for item in iterable if item] if the first argument is None); in Python 3.x, it is now equivalent to (item for item in iterable if function(item)). The subtle difference is that filter used to return a list, now it works like a generator expression - this is OK if you are only iterating over the cleaned list and discarding it, but if you really need a list, you have to enclose the filter() call with the list() constructor.
  • *These Lispy flavored constructs are considered a little alien in Python. Around 2005, Guido was even talking about dropping filter - along with companions map and reduce (they are not gone yet but reduce was moved into the functools module, which is worth a look if you like high order functions).

4) Mathematical style:

List comprehensions became the preferred style for list manipulation in Python since introduced in version 2.0 by PEP 202. The rationale behind it is that List comprehensions provide a more concise way to create lists in situations where map() and filter() and/or nested loops would currently be used.

cleaned_list = [ x for x in some_list if x is not thing ]

Generator expressions were introduced in version 2.4 by PEP 289. A generator expression is better for situations where you don't really need (or want) to have a full list created in memory - like when you just want to iterate over the elements one at a time. If you are only iterating over the list, you can think of a generator expression as a lazy evaluated list comprehension:

for item in (x for x in some_list if x is not thing):
    do_your_thing_with(item)
  • See this Python history blog post by GvR.
  • This syntax is inspired by the set-builder notation in math.
  • Python 3 has also set and dict comprehensions.

Notes

  1. you may want to use the inequality operator != instead of is not (the difference is important)
  2. for critics of methods implying a list copy: contrary to popular belief, generator expressions are not always more efficient than list comprehensions - please profile before complaining
Answer from Paulo Scardine on Stack Overflow
Top answer
1 of 8
978

1) Almost-English style:

Test for presence using the in operator, then apply the remove method.

if thing in some_list: some_list.remove(thing)

The removemethod will remove only the first occurrence of thing, in order to remove all occurrences you can use while instead of if.

while thing in some_list: some_list.remove(thing)    
  • Simple enough, probably my choice.for small lists (can't resist one-liners)

2) Duck-typed, EAFP style:

This shoot-first-ask-questions-last attitude is common in Python. Instead of testing in advance if the object is suitable, just carry out the operation and catch relevant Exceptions:

try:
    some_list.remove(thing)
except ValueError:
    pass # or scream: thing not in some_list!
except AttributeError:
    call_security("some_list not quacking like a list!")

Off course the second except clause in the example above is not only of questionable humor but totally unnecessary (the point was to illustrate duck-typing for people not familiar with the concept).

If you expect multiple occurrences of thing:

while True:
    try:
        some_list.remove(thing)
    except ValueError:
        break
  • a little verbose for this specific use case, but very idiomatic in Python.
  • this performs better than #1
  • PEP 463 proposed a shorter syntax for try/except simple usage that would be handy here, but it was not approved.

However, with contextlib's suppress() contextmanager (introduced in python 3.4) the above code can be simplified to this:

with suppress(ValueError, AttributeError):
    some_list.remove(thing)

Again, if you expect multiple occurrences of thing:

with suppress(ValueError):
    while True:
        some_list.remove(thing)

3) Functional style:

Around 1993, Python got lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed them and submitted working patches*. You can use filter to remove elements from the list:

is_not_thing = lambda x: x is not thing
cleaned_list = filter(is_not_thing, some_list)

There is a shortcut that may be useful for your case: if you want to filter out empty items (in fact items where bool(item) == False, like None, zero, empty strings or other empty collections), you can pass None as the first argument:

cleaned_list = filter(None, some_list)
  • [update]: in Python 2.x, filter(function, iterable) used to be equivalent to [item for item in iterable if function(item)] (or [item for item in iterable if item] if the first argument is None); in Python 3.x, it is now equivalent to (item for item in iterable if function(item)). The subtle difference is that filter used to return a list, now it works like a generator expression - this is OK if you are only iterating over the cleaned list and discarding it, but if you really need a list, you have to enclose the filter() call with the list() constructor.
  • *These Lispy flavored constructs are considered a little alien in Python. Around 2005, Guido was even talking about dropping filter - along with companions map and reduce (they are not gone yet but reduce was moved into the functools module, which is worth a look if you like high order functions).

4) Mathematical style:

List comprehensions became the preferred style for list manipulation in Python since introduced in version 2.0 by PEP 202. The rationale behind it is that List comprehensions provide a more concise way to create lists in situations where map() and filter() and/or nested loops would currently be used.

cleaned_list = [ x for x in some_list if x is not thing ]

Generator expressions were introduced in version 2.4 by PEP 289. A generator expression is better for situations where you don't really need (or want) to have a full list created in memory - like when you just want to iterate over the elements one at a time. If you are only iterating over the list, you can think of a generator expression as a lazy evaluated list comprehension:

for item in (x for x in some_list if x is not thing):
    do_your_thing_with(item)
  • See this Python history blog post by GvR.
  • This syntax is inspired by the set-builder notation in math.
  • Python 3 has also set and dict comprehensions.

Notes

  1. you may want to use the inequality operator != instead of is not (the difference is important)
  2. for critics of methods implying a list copy: contrary to popular belief, generator expressions are not always more efficient than list comprehensions - please profile before complaining
2 of 8
14

As a one liner:

>>> s = [u'', u'Hello', u'Cool', u'Glam']
>>> s.remove('') if '' in s else None # Does nothing if '' not in s
>>> s
['Hello', 'Cool', 'Glam']
>>> 
Discussions

python - Is there a simple way to delete a list element by value? - Stack Overflow
I want to remove a value from a list if it exists in the list (which it may not). More on stackoverflow.com
🌐 stackoverflow.com
May 8, 2010
python - remove dictionary from list if exists - Stack Overflow
I am trying to remove a dictionary from a list if it already exists but it doesn't seem to be working. Can anyone see what I am doing wrong or advise me what I should be doing new_dict = {'value': ' More on stackoverflow.com
🌐 stackoverflow.com
list.remove(value): why an error if value not in list?
API design wart. Lists are the only collection that does not allow silent object removal and I suppose that's because it's one of the oldest. For sets you can do .discard() and for dicts you can do .pop(key, None). More on reddit.com
🌐 r/Python
35
9
December 12, 2013
Help - How to remove an item in the list, if the item contains a specific letter - (multiple times)
It would be easier to construct a new list doesn't contain any sentences with 'jon' in it. For example people = ['my name is jon','his name is cameron','her name, oddly enough, is jon'] people_without_jon = [sentence for sentence in people if 'jon' not in sentence] More on reddit.com
🌐 r/learnpython
19
3
February 25, 2021
🌐
Python.org
discuss.python.org › python help
Remove elements from a list - Python Help - Discussions on Python.org
March 14, 2021 - Hi guys! I’m new in python. I just have a simple question: I have two Lists of int (the lists have different number of elements). I have to check if the elements of List2 exist in List1. If the elements already exist, I want to remove them from List1. I made this code: list1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] list2 = [9,17] for i in range(len(list1)): for j in range(len(list2)): if list2[j] == list1[i]: list1.remove(ground[i]) but I received an error massage: Index...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-remove
Python List remove() Method - GeeksforGeeks
4 days ago - The remove() method removes only the first matching occurrence. Python · a = [5, 10, 5, 15, 5] a.remove(5) print(a) Output · [10, 5, 15, 5] Explanation: a.remove(5) removes only the first occurrence of 5. The remaining occurrences stay unchanged.
🌐
Learn By Example
learnbyexample.org › python-list-remove-method
Python List remove() Method - Learn By Example
December 22, 2022 - # list comprehension L = ['red', ... L = list(filter(lambda x: x is not 'red', L)) print(L) # Prints ['green', 'blue'] remove() method raises an ValueError exception, if specified item doesn’t exist in a list....
🌐
Great Learning
mygreatlearning.com › blog › it/software development › how to remove an item from a list in python
How to Remove an Item from a List in Python
June 11, 2025 - # Create a list of numbers numbers ... is 30) del numbers[2] print(numbers) # Delete a slice (items from index 0 up to, but not including, index 2) numbers_slice = [1, 2, 3, 4, 5, 6] del numbers_slice[0:2] # Removes 1 and 2 print(numbers_slice) # Delete the entire list variable my_data = ["A", "B", "C"] del my_data # print(my_data) ...
🌐
freeCodeCamp
freecodecamp.org › news › python-list-remove-how-to-remove-an-item-from-a-list-in-python
Python List .remove() - How to Remove an Item from a List in Python
March 2, 2022 - A thing to keep in mind when using the remove() method is that it will search for and will remove only the first instance of an item. This means that if in the list there is more than one instance of the item whose value you have passed as an ...
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python remove set element if exists
Python Remove Set Element If Exists - Spark By {Examples}
May 31, 2024 - How to remove an element from Python Set by checking if an element exists? If you want to remove the element only if it exists in the set. You can use the
🌐
Programiz
programiz.com › python-programming › methods › list › remove
Python List remove()
The remove() method takes a single element as an argument and removes it from the list. If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.
🌐
Iditect
iditect.com › faq › python › how-to-delete-an-item-in-a-list-if-it-exists-in-python.html
How to delete an item in a list if it exists in python?
You can delete an item from a list in Python if it exists by using the remove() method or a list comprehension.
🌐
SitePoint
sitepoint.com › python hub › remove list items
Python - Remove List Items | SitePoint — SitePoint
Here, we first check if the element exists in the list before attempting to remove it. The pop() method removes an element at a specified index and returns its value. ... If no index is provided, it removes and returns the last element.
🌐
datagy
datagy.io › home › python posts › remove an item from a python list (pop, remove, del, clear)
Remove an Item from a Python List (pop, remove, del, clear) • datagy
December 19, 2022 - Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value. The Python del operator is similar to the Python pop method, in that it removes a list ...
🌐
Finxter
blog.finxter.com › home › learn python blog › python list remove()
Python List remove() – Be on the Right Side of Change
October 26, 2022 - This tutorial shows you everything ... language. Definition and Usage: The list.remove(element) method removes the first occurrence of the element from an existing list....
🌐
Edureka
edureka.co › blog › python-list-remove
Remove Elements From Lists | Python List remove() Method | Edureka
February 6, 2025 - To avoid this, you can use `if` statements to check if the element exists in the list before removing it. Choose the appropriate method based on your specific requirements, and ensure that you handle any potential errors that may arise while deleting elements from the list. Explore top Python interview questions covering topics like data structures, algorithms, OOP concepts, and problem-solving techniques.
🌐
Stack Abuse
stackabuse.com › bytes › fixed-the-valueerror-list-remove-x-x-not-in-list-error-in-python
[Fixed] The "ValueError: list.remove(x): x not in list" Error in Python
September 15, 2023 - Another way to handle this error is to use a try/except block. This allows you to attempt to remove the item, and if it doesn't exist, Python will execute the code in the except block instead of raising an error.
🌐
Guru99
guru99.com › home › python › remove element from a python list [clear, pop, remove, del]
Remove element from a Python LIST [clear, pop, remove, del]
August 12, 2024 - If the index given is not present, or out of range, the pop() method throws an error saying IndexError: pop index. ... The pop() method will return the element removed based on the index given. The final list is also updated and will not have the element.
🌐
Vultr Docs
docs.vultr.com › python › standard-library › list › remove
Python List Remove() - Remove Element | Vultr Docs
November 11, 2024 - python Copy · multi_fruits = ['apple', 'banana', 'cherry', 'banana'] multi_fruits.remove('banana') multi_fruits.remove('banana') print(multi_fruits) Explain Code · The method needs to be called as many times as there are occurrences of the element if the goal is to remove all instances. Prepare to catch the ValueError exception which raises when the element to remove does not exist in the list.
🌐
GeeksforGeeks
geeksforgeeks.org › python-remove-all-values-from-a-list-present-in-other-list
Remove all values from a list present in another list - Python - GeeksforGeeks
December 10, 2024 - When we work with lists in Python, sometimes we need to remove values from one list that also exists in another list. Set operations are the most efficient method for larger datasets. When working with larger lists, using sets can improve performance. This is because checking if an item exists ...