Functional approach:
Python 3.x
>>> x = [1,2,3,2,2,2,3,4]
>>> list(filter((2).__ne__, x))
[1, 3, 3, 4]
or
>>> x = [1,2,3,2,2,2,3,4]
>>> list(filter(lambda a: a != 2, x))
[1, 3, 3, 4]
or
>>> [i for i in x if i != 2]
Python 2.x
>>> x = [1,2,3,2,2,2,3,4]
>>> filter(lambda a: a != 2, x)
[1, 3, 3, 4]
Answer from Mark Rushakoff on Stack Overflow Top answer 1 of 16
732
Functional approach:
Python 3.x
>>> x = [1,2,3,2,2,2,3,4]
>>> list(filter((2).__ne__, x))
[1, 3, 3, 4]
or
>>> x = [1,2,3,2,2,2,3,4]
>>> list(filter(lambda a: a != 2, x))
[1, 3, 3, 4]
or
>>> [i for i in x if i != 2]
Python 2.x
>>> x = [1,2,3,2,2,2,3,4]
>>> filter(lambda a: a != 2, x)
[1, 3, 3, 4]
2 of 16
289
You can use a list comprehension:
def remove_values_from_list(the_list, val):
return [value for value in the_list if value != val]
x = [1, 2, 3, 4, 2, 2, 3]
x = remove_values_from_list(x, 2)
print x
# [1, 3, 4, 3]
Videos
TutorialsPoint
tutorialspoint.com › python-program-to-remove-all-occurrences-of-an-element-in-an-array-list
Python Program to Remove All Occurrences of an Element in an Array/List
May 9, 2023 - The remove() method in Python accepts ... remove all occurrences using this method, we need to compare the desired element to all other elements in the list and whenever a match occurred, we need to invoke the remove() method....
Top answer 1 of 3
7
You can use indexing:
arr = np.array([1, 2, 10, 2, 12, 2])
print(arr[arr != 2])
# [ 1 10 12]
Timing is pretty good:
from timeit import Timer
arr = np.array(range(5000))
print(min(Timer(lambda: arr[arr != 4999]).repeat(500, 500)))
# 0.004942436999999522
2 of 3
3
you can use another numpy function.It is numpy.setdiff1d(ar1, ar2, assume_unique=False). This function Finds the set difference of two arrays.
import numpy as np
a = np.array([1, 2, 10, 2,12, 2])
b = np.array([2])
c = np.setdiff1d(a,b,True)
print(c)
Python Examples
pythonexamples.org › python-remove-all-occurrences-of-item-from-list
Python List - Remove All Occurrences of an Item or Element
In the following example, we iterate through each item in the list, using Python For Loop, and when we find a match for the item to be removed, we call remove() function on the list. mylist = [21, 5, 8, 52, 21, 87] r_item = 21 # Remove the item for all its occurrences for item in mylist: if ...
w3resource
w3resource.com › python-exercises › basic › python-basic-1-exercise-75.php
Python: Remove all instances of a given value from a given array of integers and find the length of the new array - w3resource
# Define a function to remove elements equal to a given value from a list def remove_element(array_nums, val): # Initialize a variable to iterate through the list i = 0 # Iterate through the list while i < len(array_nums): # Check if the current element is equal to the given value if array_nums[i] == val: # Remove the element if it matches the given value array_nums.remove(array_nums[i]) else: # Move to the next index if the elements do not match i += 1 # Return the length of the modified list return len(array_nums) # Test the function with different lists and values, and print the results print(remove_element([1, 2, 3, 4, 5, 6, 7, 5], 5)) print(remove_element([10, 10, 10, 10, 10], 10)) print(remove_element([10, 10, 10, 10, 10], 20)) print(remove_element([], 1))
GeeksforGeeks
geeksforgeeks.org › dsa › remove-element
Remove All Occurrences of an Element in an Array - GeeksforGeeks
February 6, 2026 - DSA Python · Last Updated : 6 Feb, 2026 · Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele.
Edureka
edureka.co › blog › python-list-remove
Remove Elements From Lists | Python List remove() Method | Edureka
February 6, 2025 - There are several ways to achieve this in Python. Here are two common approaches: ... fruits = ['apple', 'banana', 'orange', 'banana', 'grapes', 'banana'] # Element to remove element_to_remove = 'banana' # Using a while loop to remove all occurrences of 'banana' while element_to_remove in fruits: fruits.remove(element_to_remove) print(fruits)
Sling Academy
slingacademy.com › article › numpy-removing-all-occurrences-of-a-value-from-an-array
NumPy: Removing all occurrences of a value from an array (4 examples) - Sling Academy
This will allow us to specifically target and remove elements that match our unwanted value. import numpy as np # Create a NumPy array arr = np.array([1, 2, 3, 4, 3, 2, 1]) # Specify the value to remove value_to_remove = 3 # Find the indexes of occurrences and delete them indexes = np.where(arr == value_to_remove) arr_removed = np.delete(arr, indexes) # Display the modified array print("Modified array:", arr_removed)
YouTube
youtube.com › watch
How to Remove all occurrences of an Element in a List in Python? - YouTube
How to Remove first occurrence of element from a List and how to remove all occurrences of a value from a List in Python #python3 #pythonforbeginners #pyt...
Published September 21, 2023
IncludeHelp
includehelp.com › python › remove-all-occurrences-a-given-element-from-the-list.aspx
Remove all occurrences of an element from a Python list
August 17, 2023 - But when you want to remove all occurrences of a given element, you need to iterate over the list elements and remove all occurrences of that element by using the list.remove() method. Consider the below example without sample input and output: Input: list = [10, 20, 10, 30, 10, 40, 10, 50] ...
GeeksforGeeks
geeksforgeeks.org › python › remove-multiple-elements-from-a-list-in-python
Remove Multiple Elements from List in Python - GeeksforGeeks
To remove multiple elements, we can use a loop to repeatedly call remove(). ... a = [10, 20, 30, 40, 50, 60, 70] remove = [20, 40, 60] for val in remove: while val in a: a.remove(val) print(a) ... We iterate over each element in remove list.
Published October 28, 2025
Team Treehouse
teamtreehouse.com › community › remove-all-instances-of-a-value-from-a-list
Remove all instances of a value from a list (Example) | Treehouse Community
November 29, 2016 - my_list = [54321, 3, 3, 3] my_list [54321, 3, 3, 3] for num in my_list: my_list.remove(3) ... my_list File "<stdin>", line 2 my_list ^ SyntaxError: invalid syntax ... I didn't get a syntax error, but it left one 3 when running the code on you're list. Seths code is cooler anyway :D Ok, I ran my code in the python shell and got that syntax error, but it did remove all the 3's.
GeeksforGeeks
geeksforgeeks.org › python › python-remove-array-item
Python Remove Array Item - GeeksforGeeks
July 23, 2025 - Removing items from an array can ... methods to remove items from an array in Python. The remove() method removes the first occurrence of a specified value from the array....
Sololearn
sololearn.com › en › Discuss › 2536505 › removing-a-valuemultiple-occurrence-from-list
Removing a value(multiple occurrence) from list
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.