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

Last updated: Apr 9, 2024
4 min

#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
```Copied!```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']
``````

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
```Copied!```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
```Copied!```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
```Copied!```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']
``````

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
```Copied!```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']
``````

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
```Copied!```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']
``````

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

shell
```Copied!```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
```Copied!```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 tolist() method converts a `numpy` array to a list.