Python Program to Remove All Occurrences of an Element in an Array/List


An array is a collection of elements of homogeneous datatypes stored in contiguous memory locations. Python does not provide support for the built-in arrays. If you need to work with arrays you need to import the "array" module or, use the arrays in numpy library.

We can use lists instead of arrays in Python. However, we cannot restrict the elements of a list to be of the same data type. 

The given task is to remove all the occurrences of an element in an array/list. i,e. we to remove the specified element including the duplicate elements. Let us understand how this actually works by considering an Input-output scenario.

Input Output Scenario

Consider a list that consists of one or more repeated elements ( duplicate elements ).

my_list = [ 1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10 ].

Now, suppose we need to remove element 10. We can clearly see that element 10 is present in the list and it is repeated 5 times. After removing all the occurrences the resulting list will be as follows -

my_list = [ 1, 20, 21, 16, 18, 22, 8 ].

There are different ways to remove elements from a Python list. Let us discuss them one by one.

Using the Remove() Method

The remove() method in Python accepts a single value, representing the element of the list as a parameter, and removes it from the current list. In order to 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.

Example

In this example, we are going to create a list of elements and remove all the occurrences of the value 10  by using the remove() method.

def removing_elements(my_list, element):
   element_count = my_list.count(element)
   for i in range(element_count):
      my_list.remove(element)
   return my_list
if __name__ == "__main__":
   my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
   element = 10
   print("The list before performing the removal operation is: ")
   print(my_list)
   result = removing_elements(my_list, element)
   print("The list after performing the removal operation is: ")
   print(result)

Output

The output for the above program is as follows −

The list before performing the removal operation is: 
[1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
The list after performing the removal operation is:
[1, 20, 21, 16, 18, 22, 8]

Using List Comprehension

The technique “List Comprehension” consists of lengthy one-line statements that can perform the entire task. Using this a new can be constructed such that the rest of the elements can be stored whenever the given base condition is satisfied. 

Here, we search for the desired element and after it is found, we constructed another list such that the matched elements are excluded ie. Except for the matched elements, all other elements will be stored within the newly constructed list which is finally considered as the resulting list.

Example

Let us see an example -

def removing_elements(my_list, element):
   result = [i for i in my_list if i != element]
   return result

if __name__ == "__main__":
   my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
   element = 10
   print("The list before performing the removal operation is: ")
   print(my_list)
   result = removing_elements(my_list, element)
   print("The list after performing the removal operation is: ")
   print(result) 

Output

The output for the above program is as follows −

The list before performing the removal operation is: 
[1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
The list after performing the removal operation is:
[1, 20, 21, 16, 18, 22, 8]

Using “ Filter() ” Method

The method filter() accepts a function and an iterable object as parameters and filters the elements of the given iterable based on the condition described by the function.

Here, using the filter() and __ne__ (functionality of the not equal operator) methods we can filter the elements of a list that are not equal to the desired element.

Example

In this example, we are removing all the occurrences of a specific element in a list by using the filter() method.

def removing_elements(my_list, element):
   result = list(filter((element).__ne__, my_list))
   return result

if __name__ == "__main__":
   my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10] 
   element = 10
   print("The list before performing the removal operation is: ")
   print(my_list)
   result = removing_elements(my_list, element)

   print("The list after performing the removal operation is: ")
   print(result)

Output

The output for the above program is as follows −

The list before performing the removal operation is: 
[1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
The list after performing the removal operation is:
[1, 20, 21, 16, 18, 22, 8]

Updated on: 09-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements