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 other list, i.e we are given some of the invalid numbers in one list which needs to be get ridden from the original list. Lets 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 the one line and hence gives an easy method to perform this particular task.
# 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]
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.
# 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]
Method #3 : Using remove()
remove() can also perform this task but only if exception of not getting specific element in handled properly. One can iterate for all the elements of remove list and remove those elements from the original list.
# 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]
Recommended Posts:
- Python | Add only numeric values present in a list
- Python | Remove None values from list
- Python | Sort the values of first list using second list
- Python | Test if all elements are present in list
- Python | Check if all the values in a list are less than a given value
- Python | Filter even values from a list
- Python | Get values of particular key in list of dictionaries
- Python | Get unique values from a list
- Python list remove()
- Python | Sort list containing alphanumeric values
- Python | Create a dictionary using list with none values
- Python | Get Unique values from list of dictionary
- Python | Summation of dictionary list values
- Python | Combining values from dictionary of list
- Python | Find indices with None values in given list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



