Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 built-in arrays, but you can use lists or import the array module. Lists are more flexible as they can contain different data types.
The task is to remove all occurrences of a specific element from a list, including duplicates. Let's explore different approaches to accomplish this ?
Input Output Scenario
Consider a list with repeated elements ?
my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
print("Original list:", my_list)
print("Element 10 appears", my_list.count(10), "times")
Original list: [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10] Element 10 appears 5 times
After removing all occurrences of 10, the resulting list will be [1, 20, 21, 16, 18, 22, 8].
Using the remove() Method
The remove() method removes the first occurrence of an element. To remove all occurrences, we count them first and call remove() repeatedly ?
def remove_all_occurrences(my_list, element):
element_count = my_list.count(element)
for i in range(element_count):
my_list.remove(element)
return my_list
# Example usage
my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
element = 10
print("Original list:", my_list)
result = remove_all_occurrences(my_list.copy(), element)
print("After removing all occurrences of", element, ":", result)
Original list: [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10] After removing all occurrences of 10 : [1, 20, 21, 16, 18, 22, 8]
Using List Comprehension
List comprehension creates a new list containing only elements that don't match the target value. This is more Pythonic and efficient ?
def remove_all_occurrences(my_list, element):
return [item for item in my_list if item != element]
# Example usage
my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
element = 10
print("Original list:", my_list)
result = remove_all_occurrences(my_list, element)
print("After removing all occurrences of", element, ":", result)
Original list: [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10] After removing all occurrences of 10 : [1, 20, 21, 16, 18, 22, 8]
Using filter() Method
The filter() function filters elements based on a condition. We use __ne__ (not equal) to keep elements that don't match the target ?
def remove_all_occurrences(my_list, element):
return list(filter((element).__ne__, my_list))
# Example usage
my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
element = 10
print("Original list:", my_list)
result = remove_all_occurrences(my_list, element)
print("After removing all occurrences of", element, ":", result)
Original list: [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10] After removing all occurrences of 10 : [1, 20, 21, 16, 18, 22, 8]
Comparison of Methods
| Method | Modifies Original | Time Complexity | Best For |
|---|---|---|---|
remove() |
Yes | O(n²) | Small lists, in-place modification |
| List Comprehension | No | O(n) | Readable, Pythonic code |
filter() |
No | O(n) | Functional programming style |
Conclusion
List comprehension is the most efficient and Pythonic approach for removing all occurrences of an element. Use remove() when you need to modify the original list in-place, and filter() for functional programming patterns.
