Remove all the occurrences of an element from a list in Python
Last Updated :
15 Jul, 2025
The task is to perform the operation of removing all the occurrences of a given item/element present in a list.
Example
Input1: 1 1 2 3 4 5 1 2 1
Output1: 2 3 4 5 2
Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1.
After removing the item, the output list is [2, 3, 4, 5, 2]
Remove all Occurrences of an Item from a Python list
Below are the ways by which we can remove all the occurrences of an Element from a List in Python:
Removing Specific Element from List using list comprehension
The list comprehension can be used to perform this task in which we just check for a match and reconstruct the list without the target element. We can create a sublist of those elements in the list that satisfies a certain condition.
Python3
def remove_items(test_list, item):
# using list comprehension to perform the task
res = [i for i in test_list if i != item]
return res
# driver code
if __name__ == "__main__":
test_list = [1, 3, 4, 6, 5, 1]
# the item which is to be removed
item = 1
print("The original list is : " + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print("The list after performing the remove operation is : " + str(res))
OutputThe original list is : [1, 3, 4, 6, 5, 1]
The list after performing the remove operation is : [3, 4, 6, 5]
Time complexity: O(n)
Auxiliary Space: O(n)
Remove all the occurrences of an element Using filter() and __ne__
We filter those items of the list which are not equal __ne__ to the item. In this example, we are using filter() and __ne__ to remove all the occurrences of an element from the list.
Python3
# Python 3 code to demonstrate
# the removal of all occurrences of
# a given item using filter() and __ne__
def remove_items(test_list, item):
# using filter() + __ne__ to perform the task
res = list(filter((item).__ne__, test_list))
return res
# driver code
if __name__ == "__main__":
test_list = [1, 3, 4, 6, 5, 1]
item = 1
# printing the original list
print("The original list is : " + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print("The list after performing the remove operation is : " + str(res))
OutputThe original list is : [1, 3, 4, 6, 5, 1]
The list after performing the remove operation is : [3, 4, 6, 5]
Time Complexity: O(n)
Auxiliary Space: O(n)
Removing Occurrences of item from a list Using remove()
In this method, we iterate through each item in the list, and when we find a match for the item to be removed, we will call remove() function on the list.
Python3
# Python 3 code to demonstrate
def remove_items(test_list, item):
# remove the item for all its occurrences
c = test_list.count(item)
for i in range(c):
test_list.remove(item)
return test_list
# driver code
if __name__ == "__main__":
test_list = [1, 3, 4, 6, 5, 1]
item = 1
# printing the original list
print("The original list is :" + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print("The list after performing the remove operation is :" + str(res))
OutputThe original list is :[1, 3, 4, 6, 5, 1]
The list after performing the remove operation is :[3, 4, 6, 5]
Time Complexity: O(n^2), where n is the length of the input list.
Auxiliary Space: O(1)
Remove Occurrences of an specific item Using replace()
In this example, we are converting the list into the string and then replacing that element string with empty space such that all occurrences of that element is removed.
Python3
#remove all occurrences of element in list
test_list = [1, 3, 4, 6, 5, 1]
ele=1
a=list(map(str,test_list))
b=" ".join(a)
b=b.replace(str(ele),"")
b=b.split()
x=list(map(int,b))
print(x)
Time Complexity: O(n)
Auxiliary Space: O(n)
Remove Occurrences of an element Using enumerate function
In this example, we are making a new list that doesn't contain any of that particular element's occurrences inside the list by using enumerate function.
Python3
test_list = [1, 3, 4, 6, 5, 1]
ele=1
x=[j for i,j in enumerate(test_list) if j!=ele]
print(x)
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
Remove all values from a list present in another list - Python 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 in a
3 min read
Remove all values from a list present in another list - Python 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 in a
3 min read
Remove Last Element from List in Python Removing the last element from a list means deleting the item at the end of list. This is often done to update or shorten the list, discard unwanted data or manage memory in certain programs.Example:lnput: [1,2,3,4,5]Output: [1,2,3,4]There are many ways to remove last element from list in python, le
2 min read
Remove Last Element from List in Python Removing the last element from a list means deleting the item at the end of list. This is often done to update or shorten the list, discard unwanted data or manage memory in certain programs.Example:lnput: [1,2,3,4,5]Output: [1,2,3,4]There are many ways to remove last element from list in python, le
2 min read