As a developer, while working on a data-cleaning project, I needed to compare two lists of customer IDs. One list came from our sales system, and the other from our support system.
The challenge was simple: I needed to find which IDs were missing in one list but present in the other.
At first, I thought there might be a direct Python built-in method for this, but there isn’t. So, I explored a few practical approaches that I’ve been using for years in Python development.
In this tutorial, I’ll show you step by step how to compare two lists in Python and return the non-matching elements. Each method is simple, and I’ll also share complete code examples.
Methods to Compare Two Lists and Return Non-Matched Elements
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 I need to get the list of all employees ‘ IDs who didn’t get promotions, I 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.
1: Use Python set() Method
First, I will use the set() method to find unmatched elements by comparing both lists in Python. A set is an unordered collection in Python that stores unique values only.
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))I executed the above example code and added the screenshot below.

In the above code, I 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.
2: Use Python’s Membership Operators(not in)
In Python, “in or not in” is a membership operator used to check the availability of an element in a collection. Using the membership operator and a for loop, I will extract the non-return matches by comparing both lists.
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)I executed the above example code and added the screenshot below.

First, I’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.
Then, I 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 = []”.
3: Use Python’s difference() Method
I can also use the difference() method, which is the method of set collection. To find the unmatched values from the lists, I have to convert list to set datatype, and then I can only use the difference() method.
Syntax:
set1.difference(set2)- set1.difference(set2): It will return the values included in set1 but not in set2.
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))I executed the above example code and added the screenshot below.

First, I 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.
4: Use List Comprehension in Python
I’ve used the same logic that we’ve used in the membership operator(2nd example); I’ve used it for a loop and not in the operator, but this time, we include them inside a list comprehension.
List comprehension is faster than using a for loop and appending the value to the empty list because it is completed in one line of code.
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)I executed the above example code and added the screenshot below.

In the above code, I 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].
Find the Unmatched Elements from Both the Lists in Python
Suppose we want to find the unmatched elements from both lists. Again, I will use the set() method to get the desired output.
Again, I’ve used the same logic as in the first example, but I want unmatched values from both lists this time.
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)I executed the above example code and added the screenshot below.

I’ve created unmatched_list1 and unmatched_list2, where I 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, I concatenate both unmatched lists, “result = unmatched_list1 + unmatched_list2”, to get all unique values in a single list.
In this Python tutorial, I helped you learn how to find the non-matched values by comparing two lists in Python.
I explained to you different methods, such as set.difference(), set(), etc., and approaches to getting the expected result, and also finding the unmatched elements from both the lists.
You may like to read:
- Check if a File Exists in Python
- Print the Contents of a File in Python
- Write to a File Without Newline in Python
- Delete a File if it Exists in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.