Remove elements from list if in another list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove elements from list if in another list in Python #

Use a list comprehension to remove the elements from a list that are present in another list, e.g. result = [item for item in my_list if item not in another_list]. The list comprehension will return a new list that doesn't contain any of the items that are in the other list.

main.py
my_list = ['apple', 'banana', 'melon', 'kiwi'] another_list = ['apple', 'melon'] # ✅ list comprehension (best) result = [item for item in my_list if item not in another_list] # 👇️ ['banana', 'kiwi'] print(result) # ------------------------ # ✅ filter() result = list(filter(lambda item: item not in another_list, my_list)) print(result) # 👉️ ['banana', 'kiwi'] # ------------------------ # ✅ using 2 set objects (conversion to set removes duplicates) # set objects are unordered collections of items result = list(set(my_list) - set(another_list)) print(result) # 👉️ ['kiwi', 'banana']

We used a list comprehension to remove the items from one list that occur in another list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is not present in the other list and return the result.

main.py
my_list = ['apple', 'banana', 'melon', 'kiwi'] another_list = ['apple', 'melon'] result = [item for item in my_list if item not in another_list] # 👇️ ['banana', 'kiwi'] print(result)

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

The list comprehension doesn't remove items from the original list, it returns a new list that only contains items that are not present in the other list.

Alternatively, you can use the filter() function.

To remove the elements from a list that are present in another list:

  1. Use the filter() function to filter out any elements that are present in the other list.
  2. Use the list() class to convert the filter object to a list.
  3. The new list won't contain items that are present in the other list.
main.py
my_list = ['apple', 'banana', 'melon', 'kiwi'] another_list = ['apple', 'melon'] result = list( filter(lambda item: item not in another_list, my_list) ) print(result) # 👉️ ['banana', 'kiwi']

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The lambda function checks if the current item is not present in the other list and returns the result.

The filter object contains only the elements that satisfy the condition.

The last step is to use the list() class to convert the filter object to a list.

An alternative and more performant approach is to use two set objects.

To remove the elements from a list that are present in another list:

  1. Use the set() class to convert the two lists to set objects.
  2. Subtract the other set from the first set to get the difference.
  3. Use the list() class to convert the difference to a list.
main.py
my_list = ['apple', 'banana', 'melon', 'kiwi'] another_list = ['apple', 'melon'] result = list(set(my_list) - set(another_list)) print(result) # 👉️ ['kiwi', 'banana']

We used the set() class to convert the two lists to set objects.

Set objects are an unordered collection of unique elements.

When a list that contains duplicates is converted to a set, all of the duplicates are dropped.

Another thing to note is that set objects are unordered, so you shouldn't use this approach if you need to preserve the order.

When we subtract one set from another, we get the difference.

main.py
my_list = ['apple', 'banana', 'melon', 'kiwi'] another_list = ['apple', 'melon'] # 👇️ {'kiwi', 'banana'} print(set(my_list) - set(another_list))

You can also use the set.difference() method to achieve the same result.

main.py
my_list = ['apple', 'banana', 'melon', 'kiwi'] another_list = ['apple', 'melon'] result = list(set(my_list).difference(another_list)) print(result) # 👉️ ['kiwi', 'banana']

Set objects have a difference() method that returns a new set with elements in the set that are not in the provided iterable.

In other words, set_1.difference(set_2) returns a new set with elements from set_1 that are not present in set_2.

You should only use the set objects approach if:

  • you are OK with dropping any duplicate values when converting the list to a set
  • you don't need to preserve the order
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.