Remove all list items from a list that exist in another list in Python
Imagine an eCommerce website that stores a list of products in its database. Now consider another list in the same database that contains products under the category of sports. This list can be considered as a part of the original products list. If invalid items exist in the second list, then it needs to be removed from the original product lists as well. Thus, in this tutorial, the following methods describe how to remove all items from a list that exists in another list on python.
Method 1: Using remove() function
The remove() function is an in-built function in Python, which helps us to remove items from a list. The syntax of the remove() function is:
list.remove(element)
The function takes the element that is to be removed as the argument. It searches the entire list and removes the first occurrence of the element. If it does not find the element in the list, it throws a ValueError Exception.
We use the remove() function to remove all list items from a list that exists in the other list through the following code:
products_sport = ["candy", "knife"] products= ["candy", "ball", "bike","badminton","knife"] for item in products_sport: if item in products: products.remove(item) print(f"Items in product list are: {products}")
Output:
Items in product list are: ['ball', 'bike', 'badminton']
In the above code, we have two lists: products and products_sport. We want to remove all items from product_sport that exist in products. To do this, we use for loop and iterate over every item present in the product_sport list. Then we check whether that item is present in the products list. If it is present, then remove that item from the products list using the remove() function.
Method 2: Using set() function in Python
Set() function in Python takes an iterable as an argument and converts that iterable into a set object. An iterable can be anything such as a list, dictionary or tuple, etc. A set object is an unordered collection of items that are unique and cannot be changed. The syntax of the set() function in Python is:
set(iterable)
We use the set() function to remove all list items from a list that exists in the other list through the following code:
products_sport = ["candy", "knife"] products= ["candy", "ball", "bike","badminton","knife","knife"] products=set(products) - set(products_sport) print(f"Items in product list are: {list(products)}")
Output:
Items in product list are: ['ball', 'badminton', 'bike']
In the above code, we use the set() function to convert both the lists (products & products_sport) into set objects. We subtract the items of the set(product_sport) from the set(products). This will remove the elements of the product_sport list from the products list. We convert the result generated back into a list using the list() function and print the desired output.
Method 3:Using list comprehension
List comprehensions in Python use iterables like tuples, strings, lists, etc, and give a new list. A list comprehension consists of brackets containing the expression, that is executed for each element and a for loop that iterates over every element of the iterable. There is an optional IF statement which adds items to the list only if the condition is satisfied. The syntax of the list comprehension in Python is:
list2= [ expression for element in iterable if condition == True ]
We use list comprehension in Python to remove all list items from a list that exists in the other list through the following code:
products_sport = ["candy", "knife"] products= ["candy", "ball", "bike","badminton","knife","knife"] products = [item for item in products if item not in products_sport] print(f"Items in product list are: {products}")
Output:
Items in product list are: ['ball', 'badminton', 'bike']
In the above code, the for loop iterates over every element of the products list. The IF condition checks whether the item is present in the products_sports list or not. If it is not present, then we append that item to the products list. Thus, we get only those items in the products list that are not present in the products_sport list.
Method 4: Using filter() and lambda() functions in Python
The filter function takes a function and iterable as the arguments. It filters the iterable based on the function and returns the filtered result. The iterables can be anything such as lists, arrays, etc. The syntax for the filter() function in Python is:
filter(function, iterable)
The lambda() function is a function with no name. It takes any number of arguments as input and contains an expression. The expression is evaluated and if it is true then it is returned. The syntax for the lambda() function in Python is:
lambda arguments: expression
We use filer() and lambda() functions in Python to remove all list items from a list that exists in the other list through the following code:
products_sport = ["candy", "knife"] products= ["candy", "ball", "bike","badminton","knife","knife"] products=filter(lambda item: item not in products_sport, products) print(f"Items in product list are: {list(products)}")
Output:
Items in product list are: ['ball', 'badminton', 'bike']
In the above code, we use a filter function that will take a lambda function and the products list as arguments. It will filter the products list based on the lambda function. The lambda function will take every item of the products list as an argument. The expression of the lambda function is an IF condition that evaluates whether the item is present in the products_sport list or not. If it is not present, only then the item is returned by the lambda function and appended to the products list by the filter function.
Thus, we have reached the end of the tutorial on how to remove all list items from a list that exists in another list in Python. You can use the following links to learn more about the lambda function or list comprehension in python.
lambda function in Python: Lambda Function In Python
List comprehension in Python: List and dictionary comprehension in python
Leave a Reply