How to compare two lists in Python and return non-matches elements

In this tutorial, you will learn “How to compare two lists in Python and return non-matches values” with practical examples.

While working on a large amount of data, I was required to compare two columns(lists) and extract the unmatched elements from the lists. I found some different techniques for comparing two lists and returning non-matching values in Python.

I thought I would share this information with you so that you can learn about all the different approaches and realistic examples in this Python tutorial.

In this article, I have explained 5 different methods for finding the unmatched elements in a list in Python.

Compare two lists and return non-matches elements in Python

Suppose there are two different lists of employee_id’s like:

emp_id = [1, 2, 3, 4, 5, 6, 7, 8]
promoted_emp_id  = [3, 5, 6, 8]

If we need to get the list of all employees ‘ IDs who didn’t get promotions, we can find the difference between the two lists in Python to get this output type.

[1, 2, 4, 7]

Let’s understand it with some realistic examples and five different methods.

How to find the difference between two lists in Python using the set() method

First, we will use the set() method to find unmatched elements by comparing both lists in Python. Set is an unordered collection in Python that stores unique values only.

READ:  PyTorch Add Dimension [With 6 Examples]

When you need to compare multiple collections-related tasks, set() makes it easier(if you don’t care about the sequence).

syntax

set(list1) - set(list2)
  • set(list1) – set(list2): Here, it will perform type conversion from list to set and remove duplicate values by comparing list1 and list2.
emp_id = [1,2,3,4,5,6,7,8,9,10]
promoted_employees = [1,3,4,6,8,10]

not_promoted = list(set(emp_id) - set(promoted_employees))

print(sorted(not_promoted))
How to find the difference between two lists in Python using the set() method

In the above code, we have two different lists: one is the list of employee IDs, and the other is the list of promoted employees. Now, I want to get the list of the employees who have not been promoted.

So, we’ve used “set(emp_id) – set(promoted_employees)” to convert both the lists to the set and subtract “emp_id from this promoted_employees to remove the duplicates from both the list and unique values left only.

Python find non-matches values in two lists using membership operators(not in)

In Python, “in or not in” is a membership operator used to check the availability of an element in the collection. Using the membership operator and for loop, we will extract the non-return matches by comparing both lists.

Let’s understand this with a practical example of How to find unmatched elements from the lists in Python.

list_of_countries = ["France", "Germany", "Italy", "Japan", "Spain", "United Kingdom", "United States", "China"]

traveled_countries = ["Germany", "Spain", "Italy", "China"]

remaining_countries = []

for country in list_of_countries:
    if country not in traveled_countries:
        remaining_countries.append(country)

print("Remaining countries to be travel : ", remaining_countries)
Python find non-matches values in two lists using member operators

In the code, there is a list of countries and a list of countries that have been travelled. You want to find the remaining countries, so First, we’ve initialized the country variable inside the for loop.
for country in list_of_countries“, it will target all the elements of list_of_countries one by one.

READ:  Matplotlib set y axis range

Then, we used the “if country not in traveled_countries” membership operators, checked if the country was not included in the travelled countries, and added that country to the “remaining_countries = []”.

Compare two lists of strings in Python and get unmatched values using the difference() method

We can also use the difference() method, which is the method of set collection. To find the unmatched values from the lists, we have to convert our list to the set datatype, and then only you can use the difference() method.

Syntax

set1.difference(set2)
  • set1.difference(set2): It will return the values included in set1 but not in set2.

Let’s understand how we can compare two lists and get the difference between two lists in Python

list_of_states = ["California", "Texas", "Florida", "New York", "Illinois", "Georgia"]
selected_states = ["Texas", "New York", "Georgia"]

remaining_states = set(list_of_states).difference(selected_states)

print(list(remaining_states))
Compare two lists of strings in Python and get unmatched values using the difference() method

In the above code, we’ve two different lists, which are list_of_states and selected_states, so first, we converted them to sets to use the difference() method like this
remaining_states = set(list_of_states).difference(selected_states). It will compare both the sets and remove values included in selected_states from the list_of_states.

Find non-matches elements in Python using List Comprehension

We’ve used the same logic that we’ve used in the membership operator(2nd example); we’ve used it for loop and not in the operator, but this time, we include them inside list comprehension.

List comprehension is faster than using a normal for loop and appending the value to the empty list because it is completed in one line of code.

Let’s see how we can extract the non-repeated values by comparing two lists in Python.

emp_id = [1,2,3,4,5,6,7,8,9,10]

targeted_employees = [1,3,4,6,8,10]

remaining_employees = [x for x in emp_id if x not in targeted_employees]

print(remaining_employees)
Find non-matches elements in Python using List Comprehension

In the above code, we have included two lists and used list comprehension to filter out the values that are not in the target_employees list, like this: [x for x in emp_id if x is not in targeted_employees].

READ:  Horizontal line matplotlib

How to find the unmatched elements from both the lists in Python

Suppose, you want to find the unmatched elements from both lists, again we will use the set() method to get the desired output.

Again, we’ve used the same logic as in the first example, but we want unmatched values from both lists this time.

Let’s see how to find unique values from both lists in Python.

list1 = [2,4,6,8,12,20]
list2 = [2,4,5,6,8,10]

unmatched_list1 = list(set(list1) - set(list2))
unmatched_list2 = list(set(list2) - set(list1))

result = unmatched_list1 + unmatched_list2

print(result)
How to find the unmatched elements from both the lists in Python

In the above code, we have lists 1 and 2. If we compare both lists, some values are the same, and some are unique. After comparing both lists, we needed a list of all unique values.

So we’ve created unmatched_list1 and unmatched_list2, where we will store the unique values from both lists, such as “unmatched_list1 = list(set(list1)—set(list2))” and “unmatched_list2 = list(set(list2)—set(list1))”

Finally, we concatenate both unmatched lists, “result = unmatched_list1 + unmatched_list2”, to get all unique values in a single list.

Conclusion

In this Python tutorial, you learned to find the non-matched values by comparing both lists.

You learned different methods, such as set.difference(), set(), etc., and approaches to getting the expected result.

You may like to read: