In this tutorial, you’ll discover 3 simple ways to find the difference between two Python lists.
How to Find the Difference Between Two Lists
One of the methods is using the Python Set. It works by converting the lists into sets and separating the unique parts.
By the way, if you are not aware of the sets in Python, read about it first. It would quickly introduce you to how Python implements the mathematical form of a Set. There are other non-set ways to do this, we’ll be discussing them below.
Python Set seems to be the most obvious choice to identify the differences between the two lists. So, let’s explore it first and then explore other ways: Nested loops and comprehension.
Simple methods to find the difference between two lists
Before we start the solution part, let’s define the test parameters, i.e., the two lists we’ll use to find the difference.
Test data
# Test Input list_a = [11, 16, 21, 26, 31, 36, 41] list_b = [26, 41, 36]
And we want our solution to provide the following output:
# Expected Result # list_out = list_a - list_b list_out = [11, 21, 31, 16]
Let’s quickly go through each of the 3 methods and understand them in detail.
Method:1 Use set() operations
In this approach, we’ll first derive two SETs (say set1 and set2) from the LISTs (say list1 and list2) by passing them to the set() function. After that, we’ll perform the set difference operation. It will return those elements from LIST1 which don’t exist in the second.
Here is the sample Python program to get the difference between two lists.
""" Desc: Using set() to find the difference between two lists in Python """ def list_diff(list1, list2): return (list(set(list1) - set(list2))) # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print(list_diff(list1, list2))
After running this, you should see the following outcome:
[16, 11, 21, 31]
Method:2 Using nested loops
In this method, we’ll use nested For Loop to compare each element of the first list with the second. And while traversing, we’ll be appending every non-matching item to a new (and empty) list.
The new list would finally include the difference between the two given sequences. Below is the sample program to demonstrate this approach.
""" Desc: Nested loop to find the difference between two lists in Python """ def list_diff(list1, list2): out = [] for ele in list1: if not ele in list2: out.append(ele) return out # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print(list_diff(list1, list2))
After running the above program, you should see the following outcome:
[11, 16, 21, 31]
Method:3 Using list comprehension
It is almost a similar technique that we used in the previous one. Here, we replaced the nested loops with the list comprehension syntax.
See the example below.
""" Desc: List comprehension to find the difference between two lists in Python """ def list_diff(list1, list2): out = [item for item in list1 if not item in list2] return out # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print(list_diff(list1, list2))
After running the above program, you should see the following outcome:
[11, 16, 21, 31]
Before you leave
Hopefully, you have learned several ways to check the difference between two lists. However, you may practice more with examples to gain confidence.
Also, if you want to learn more important Python topics from scratch to depth, go through this step-by-step Python tutorial.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.