Open In App

Remove all the occurrences of an element from a list in Python

Last Updated : 08 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output

The 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))


Output

The 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))


Output

The 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)


Output

[3, 4, 6, 5]

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)


Output

[3, 4, 6, 5]

Time Complexity: O(n)
Auxiliary Space: O(n)



Similar Reads

Python | Remove all occurrences in nested list
The task of removing an element generally doesn't pose any challenge, but sometimes, we may have a more complex problem than just removing a single element or performing removal in just a normal list. The problem can be removing all occurrences of the nested list. Let's discuss certain ways in which this problem can be solved. Method #1: Using list
5 min read
Python - Get the indices of all occurrences of an element in a list
In this article, we will be going to understand how to get all the indices of an element in a list in Python and we will also cover a few of the best approaches to do so. Example Input: my_list = [1, 2, 3, 1, 5, 4] item = 1Output: 0,3Explanation: The output contains all the index of element 1 in the my_list.Find List Index of All Occurrences of an
5 min read
Python | Count occurrences of an element in a list
Given a list in Python and a number x, count the number of occurrences of x in the given list. Examples: Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 Explanation: 10 appears three times in given list. Input: lst = [8, 6, 8, 10, 8, 20, 10, 8, 8], x = 16 Output: 0 Explanation: 16 appears zero times in given list.Count Occurrences
4 min read
Python - All occurrences of Substring from the list of strings
Given a list of strings and a list of substring. The task is to extract all the occurrences of a substring from the list of strings. Examples: Input : test_list = ["gfg is best", "gfg is good for CS", "gfg is recommended for CS"] subs_list = ["gfg", "CS"] Output : ['gfg is good for CS', 'gfg is recommended for CS'] Explanation : Result strings have
5 min read
Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
Python | Count occurrences of an element in a Tuple
In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple: (10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2)Input : 4
3 min read
Python | Deleting all occurrences of character
These days string manipulation is very popular in Python, and due to its immutable character, sometimes, it becomes more important to know its working and hacks. This particular article solves the problem of deleting all occurrences of a character from a string. Let's discuss ways in which this can be achieved. Method #1: Using translate() Usually
4 min read
Python | Get the starting index for all occurrences of given substring
Given a string and a substring, the task is to find out the starting index for all the occurrences of a given substring in a string. Let's discuss a few methods to solve the given task. Method #1: Using Naive Method C/C++ Code # Python3 code to demonstrate # to find all occurrences of substring in # a string # Initialising string ini_string = 'xbze
3 min read
Python - Replace all repeated occurrences with N
Sometimes, while working with Python list, we can have a problem in which we need to replace an element with another. But one can have variations of these such as increase of number and keeping the first occurrence. This can have applications in various domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using enumer
4 min read
Python - Replace all occurrences of a substring in a string
Sometimes, while working with Python strings, we can have a problem in which we need to replace all occurrences of a substring with other. Input : test_str = "geeksforgeeks" s1 = "geeks" s2 = "abcd" Output : test_str = "abcdforabcd" Explanation : We replace all occurrences of s1 with s2 in test_str. Input : test_str = "geeksforgeeks" s1 = "for" s2
3 min read
Practice Tags :
three90RightbarBannerImg