Find elements in one List that are not in the other (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Find elements in one list that are not in the other (Python)
  2. Find elements in one list that are not in the other using list comprehension
  3. Find elements in one list that are not in the other using a for loop
  4. Find elements in one list that are not in the other using NumPy

# Find elements in one list that are not in the other (Python)

To find the elements in one list that are not in the other:

  1. Use the set() class to convert the first list to a set object.
  2. Use the difference() method to get the elements in the set that are not in the list.
  3. Use the list() class to convert the set object to a list.
main.py
list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] result_1 = list(set(list_2).difference(list_1)) print(result_1) # ๐Ÿ‘‰๏ธ ['e', 'd'] # -------------------------------------- result_2 = list(set(list_1).difference(list_2)) print(result_2) # ๐Ÿ‘‰๏ธ ['b', 'c']

find elements in one list that are not in the other

The code for this article is available on GitHub

The first step is to use the set() class to convert the list to a set object.

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(list_2).difference(list_1) returns a new set that contains the items in list_2 that are not in list_1.

main.py
list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] # ๐Ÿ‘‡๏ธ {'d', 'e'} print(set(list_2).difference(list_1))

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

main.py
list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] result_1 = list(set(list_2).difference(list_1)) print(result_1) # ๐Ÿ‘‰๏ธ ['d', 'e']

Alternatively, you can use a list comprehension.

# Find elements in one list that are not in the other using list comprehension

This is a three-step process:

  1. Use a list comprehension to iterate over the list.
  2. Check if each element is not contained in the other list and return the result.
  3. The new list will only contain items from the first list that are not in the second list.
main.py
list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] result_1 = [item for item in list_2 if item not in list_1] print(result_1) # ๐Ÿ‘‰๏ธ ['d', 'e'] # -------------------------------------- result_2 = [item for item in list_1 if item not in list_2] print(result_2) # ๐Ÿ‘‰๏ธ ['b', 'c']

find elements in one list that are not in other using list comprehension

The code for this article is available on GitHub

We used a list comprehension to iterate over one of the lists.

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 item is not contained in the other list and return the result.

The new list contains all of the elements in the first list that are not in the second.

In other words, [item for item in list_2 if item not in list_1] returns all of the elements in list_2 that are not in list_1.

# Find elements in one list that are not in the other using a for loop

You can also use a for loop to find elements in one list that are not in the other.

main.py
list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] new_list = [] for item in list_2: if item not in list_1: new_list.append(item) print(new_list) # ๐Ÿ‘‰๏ธ ['d', 'e']

find elements in one list that are not in other using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the second list.

On each iteration, we check if the current item is not contained in the first list.

If the condition is met, we append the item to a new list.

The new list only contains the items from the second list that are not contained in the first.

# Find elements in one list that are not in the other using NumPy

You can also use the NumPy module to find the elements in one list that are not in the other.

main.py
import numpy as np list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] result_1 = np.setdiff1d(list_2, list_1) print(result_1) # ๐Ÿ‘‰๏ธ ['d' 'e'] result_2 = np.setdiff1d(list_1, list_2) print(result_2) # ๐Ÿ‘‰๏ธ ['b' 'c']

find elements in one list that are not in other using numpy

The code for this article is available on GitHub

Make sure to install NumPy to be able to import and use the module.

shell
pip install numpy pip3 install numpy

The numpy.setdiff1d method finds the set difference between two arrays.

The method returns the unique values in the first array that are not contained in the second array.

If you need to convert the results array to a native Python list, use the tolist() method.

main.py
import numpy as np list_1 = ['a', 'b', 'c'] list_2 = ['a', 'd', 'e'] result_1 = np.setdiff1d(list_2, list_1).tolist() print(result_1) # ๐Ÿ‘‰๏ธ ['d', 'e'] print(type(result_1)) # ๐Ÿ‘‰๏ธ <class 'list'>
The code for this article is available on GitHub

The tolist() method converts a numpy array to a list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev