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.
Example
Input: List one is : [1, 3, 4, 6, 7] List two is : [3, 6] Output: The list after performing the remove operation is : [1, 4, 7]
Using list comprehension to remove all values from a list present in other list
The list comprehension can be used to perform the naive method in just one line and hence gives an easy method to perform this particular task.
Python3
# Python 3 code to demonstrate # to remove elements present in other list # using list comprehension # initializing list test_list = [ 1 , 3 , 4 , 6 , 7 ] # initializing remove list remove_list = [ 3 , 6 ] # printing original list print ( "The original list is : " + str (test_list)) # printing remove list print ( "The original list is : " + str (remove_list)) # using list comprehension to perform task res = [i for i in test_list if i not in remove_list] # printing result print ( "The list after performing remove operation is : " + str (res)) |
Output :
The original list is : [1, 3, 4, 6, 7] The original list is : [3, 6] The list after performing remove operation is : [1, 4, 7]
Using filter() + lambda to remove all values from a list present in other list
The filter function can be used along with lambda to perform this task and creating a new filtered list of all the elements that are not present in the remove element list.
Python3
# Python 3 code to demonstrate # to remove elements present in other list # using filter() + lambda # initializing list test_list = [ 1 , 3 , 4 , 6 , 7 ] # initializing remove list remove_list = [ 3 , 6 ] # printing original list print ( "The original list is : " + str (test_list)) # printing remove list print ( "The original list is : " + str (remove_list)) # using filter() + lambda to perform task res = filter ( lambda i: i not in remove_list, test_list) # printing result print ( "The list after performing remove operation is : " + str (res)) |
Output :
The original list is : [1, 3, 4, 6, 7] The original list is : [3, 6] The list after performing remove operation is : [1, 4, 7]
Using remove() to remove all values from a list present in other list
remove() can also perform this task but only if the exception of not getting specific elements is handled properly. One can iterate for all the elements of the removed list and remove those elements from the original list.
Python3
# Python 3 code to demonstrate # to remove elements present in other list # using remove() # initializing list test_list = [ 1 , 3 , 4 , 6 , 7 ] # initializing remove list remove_list = [ 3 , 6 ] # printing original list print ( "The original list is : " + str (test_list)) # printing remove list print ( "The original list is : " + str (remove_list)) # using remove() to perform task # handled exceptions. for i in remove_list: try : test_list.remove(i) except ValueError: pass # printing result print ( "The list after performing remove operation is : " + str (test_list)) |
Output :
The original list is : [1, 3, 4, 6, 7] The original list is : [3, 6] The list after performing remove operation is : [1, 4, 7]
Using set() to remove all values from a list present in other list
set() can be used to perform this task and creating a new filtered list of all the elements that are not present in the remove element list.
Python3
# Python 3 code to demonstrate # to remove elements present in other list # using set() # initializing list test_list = [ 1 , 3 , 4 , 6 , 7 ] # initializing remove list remove_list = [ 3 , 6 ] # printing original list print ( "The original list is : " + str (test_list)) # printing remove list print ( "The original list is : " + str (remove_list)) # using set() to perform task set1 = set (test_list) set2 = set (remove_list) res = list (set1 - set2) # printing result print ( "The list after performing remove operation is : " + str (res)) |
Output :
The original list is : [1, 3, 4, 6, 7] The original list is : [3, 6] The list after performing remove operation is : [1, 4, 7]
Using the itertools module to remove all values from a list present in other list
Here we are using the itertools module to remove the elements of a list from another list in Python.
Python3
from itertools import filterfalse test_list = [ 1 , 3 , 4 , 6 , 7 ] remove_list = [ 3 , 6 ] # printing original list print ( "The original list is : " + str (test_list)) # printing remove list print ( "The original list is : " + str (remove_list)) res = list (filterfalse(remove_list.__contains__, test_list)) # printing result print ( "The list after performing remove operation is : " + str (res)) |
Output:
The original list is : [1, 3, 4, 6, 7] The original list is : [3, 6] The list after performing remove operation is : [1, 4, 7]
Using set difference:
One way to remove all values from a list present in another list is to use a combination of sets and list comprehension. First, you can convert both lists to sets and then use the difference method to find the elements in the first list that are not present in the second list. Finally, you can use list comprehension to convert the resulting set back to a list.
Here is an example of how this can be done:
Python3
# initializing lists original_list = [ 1 , 3 , 4 , 6 , 7 ] remove_list = [ 3 , 6 ] # using difference() to remove values from original_list that are present in remove_list filtered_list = list ( set (original_list).difference(remove_list)) # printing result print (filtered_list) # Output: [1, 4, 7] #This code is contributed by Edula Vinay Kumar Reddy |
[1, 4, 7]
This approach has a time complexity of O(n) and a space complexity of O(n), where n is the length of the longer list. It is a relatively efficient way to remove all values from a list present in another list.
Please Login to comment...