To remove the first occurrence of an element, use list.remove:
>>> xs = ['a', 'b', 'c', 'd']
>>> xs.remove('b')
>>> print(xs)
['a', 'c', 'd']
To remove all occurrences of an element, use a list comprehension:
>>> xs = ['a', 'b', 'c', 'd', 'b', 'b', 'b', 'b']
>>> xs = [x for x in xs if x != 'b']
>>> print(xs)
['a', 'c', 'd']
Answer from Johannes Charra on Stack OverflowStack Overflow
stackoverflow.com › questions › 4915920 › how-to-delete-an-item-in-a-list-if-it-exists
python - How to delete an item in a list if it exists? - Stack Overflow
I am getting new_tag from a form text field with self.response.get("new_tag") and selected_tags from checkbox fields with self.response.get_all("selected_tags") I combine them like this: tag_str
Remove elements from a list
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... More on discuss.python.org
python - Is there a simple way to delete a list element by value? - Stack Overflow
Technically, this comprehension does not remove an element. It creates a new list, which might be a problem if the list is an argument to the function or typed as Final. The fix is simple: instead of xs = ..., use xs[:] = .... 2023-01-06T16:59:23.397Z+00:00 ... Save this answer. ... Show activity on this post. Usually Python ... More on stackoverflow.com
python - Remove items from list if it exists - Stack Overflow
My code will not delete the items if they exist in the list. This is the list I am working with: [1, 2, 3, 4, 5, 6, 7, 8] Run the function: remove_items_from_list(my_list, [1,5,6]) This is the O... More on stackoverflow.com
How to remove items from one list and put them in another list while in a For loop?
if you want to remove specific items and put them in a list just .append and .remove them, if you want to use .pop that also works. More on reddit.com
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 › remove-item-from-list-in-python
How to Remove Item from a List in Python - GeeksforGeeks
For example, remove() can be used to remove the first occurrence of a specified value from a list. ... remove() method removes the first occurrence of a specified value from the list. If the value appears multiple times, only the first occurrence ...
Published: July 15, 2025
Top answer 1 of 16
1914
To remove the first occurrence of an element, use list.remove:
>>> xs = ['a', 'b', 'c', 'd']
>>> xs.remove('b')
>>> print(xs)
['a', 'c', 'd']
To remove all occurrences of an element, use a list comprehension:
>>> xs = ['a', 'b', 'c', 'd', 'b', 'b', 'b', 'b']
>>> xs = [x for x in xs if x != 'b']
>>> print(xs)
['a', 'c', 'd']
2 of 16
216
Usually Python will throw an Exception if you tell it to do something it can't so you'll have to do either:
if c in a:
a.remove(c)
or:
try:
a.remove(c)
except ValueError:
pass
An Exception isn't necessarily a bad thing as long as it's one you're expecting and handle properly.
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 - The condition was that if an item in the original list has a value of Python, it would not be part of the sublist. Now, if you don't want to create a new list, but instead want to modify the already existing list in-place, then use the slice assignment combined with list comprehension.
GeeksforGeeks
geeksforgeeks.org › python › python-list-remove
Python List remove() Method - GeeksforGeeks
July 17, 2026 - Example 2: Here, the same value appears multiple times in the list. The remove() method removes only the first matching occurrence. ... Explanation: a.remove(5) removes only the first occurrence of 5. The remaining occurrences stay unchanged.
GUVI
guvi.in › blog › programming languages › remove element from python list: 5 methods with code + speed comparison
How to remove an element from a list in Python (2026 Guide)
August 17, 2026 - You would need to iterate through each sublist and remove elements using any of the methods mentioned. Not exactly. my_list.clear() empties the list in place, so all references to that list object still point to the same (now empty) list. Assigning my_list = [] creates a brand new list object, which other references to the old list won’t see. Python raises a ValueError. To avoid this, check if the value exists first using the “in” operator: if “apple” in my_list: my_list.remove(“apple”).
Boot.dev
blog.boot.dev › python › complete guide to removing elements from lists in python
Complete Guide to Removing Elements From Lists in Python | Boot.dev
December 9, 2021 - primes = [2, 3, 5, 5, 7, 11] primes.remove(5) print(primes) # [2, 3, 5, 7, 11] primes.remove(5) # [2, 3, 7, 11] primes.remove(5) # careful, this will throw an error # ValueError: list.remove(x): x not in list · If you want to safely remove items, and you aren't sure if they exist in the list or not, you can either catch the error:
Top answer 1 of 3
3
if [items] in list checks that: a list containing the list of items is an element of the list. That is, you are asking: is [[1, 2, 3]] a member of the list? Probably not.
What you want to do is iterate over the element of items_to_remove and do what you did
for item in items_to_remove:
if item in list:
list.remove(item)
2 of 3
2
Try this :
def remove_items_from_list(ordered_list, items_to_remove):
return [i for i in ordered_list if not i in items_to_remove]