Python | Remove all values from a list present in other list

20 Aug, 2020
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 needs to be get ridden from the original list. Let’s discuss various ways in which this can be performed.Method #1: Using list comprehension 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 listtest_list = [1, 3, 4, 6, 7] # initializing remove listremove_list = [3, 6] # printing original listprint ("The original list is : " + str(test_list)) # printing remove listprint ("The original list is : " + str(remove_list)) # using list comprehension to perform taskres = [i for i in test_list if i not in remove_list] # printing resultprint ("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]


Method #2 : Using filter() + lambda 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 listtest_list = [1, 3, 4, 6, 7] # initializing remove listremove_list = [3, 6] # printing original listprint ("The original list is : " + str(test_list)) # printing remove listprint ("The original list is : " + str(remove_list)) # using filter() + lambda to perform taskres = filter(lambda i: i not in remove_list, test_list) # printing resultprint ("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]


Method #3 : Using remove() 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 listtest_list = [1, 3, 4, 6, 7] # initializing remove listremove_list = [3, 6] # printing original listprint ("The original list is : " + str(test_list)) # printing remove listprint ("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 resultprint ("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]


Method #4: Using set() 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 listtest_list = [1, 3, 4, 6, 7] # initializing remove listremove_list = [3, 6] # printing original listprint ("The original list is : " + str(test_list)) # printing remove listprint ("The original list is : " + str(remove_list)) # using set() to perform taskset1 = set(test_list)set2 = set(remove_list)res = list(set1 - set2) # printing resultprint ("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]


 
ritesh_nehru
Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.