In this tutorial, we’ll discover two Pythonic ways to find the Difference Between Two Lists. One of the methods is using the Python Set. It first converts the lists into sets and then gets the unique part out of that.
Other non-set methods compare two lists element by element and collect the unique ones. We can implement these by using nested for loops and with the list comprehension.
By the way, if you are not aware of the sets in Python, then follow the below tutorial. It would quickly introduce you to how Python implements the mathematical form of Set.
Pythonic Ways to Find the Difference Between Two Lists
Python Set seems to be the most obvious choice to identify the common as well as the difference of two lists. So, we are going to explore it first and then will use nested loops and list comprehension.
Before we move on to the solution part, let’s define the test parameters, i.e., the two lists which we have to find the difference.
# 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 start to create a program to find the difference between two lists, first using sets.
Use set() to find the difference of two lists
In this approach, we’ll first derive two SETs (say set1 and set2) from the LISTs (say list1 and list2) by passing them to 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 of 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]
Without set(), 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]
Without set(), 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]
We hope that after wrapping up this tutorial, you should know several ways to check two lists for the difference. However, you may practice more with examples to gain confidence.
Also, to learn Python from scratch to depth, do read our step by step Python tutorial.