Get the Difference Between Two Lists in Python

Use
set.difference()
to Find the Difference Between Two Lists in Python 
Use
set.symmetric_difference()
to Find the Difference Between Two Lists in Python 
Use the Keyword
in
to Find the Difference Between the Two Lists in Python 
Use the
NumPy
Library to Find the Difference Between Two Lists in Python
There are many operations performed on data structures like lists, dictionaries, tuples, and arrays. In Python, these data structures are used in almost every program. One of these data structures is the lists in Python. Lists in Python programming helps in storing more than one item in just one variable. Finding out the difference between two lists is a very common task that almost every programmer carries out.
This tutorial will demonstrate several ways to get the difference between two lists in Python.
Use set.difference()
to Find the Difference Between Two Lists in Python
The set()
method helps the user to convert any iterable to an iterable sequence, which is also called a set. The iterables can be a list, a dictionary, or a tuple.
The set.difference()
function is used to return the difference between the two sets. This function basically eliminates the common elements in two sets.
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
difference_1 = set(list_1).difference(set(list_2))
difference_2 = set(list_2).difference(set(list_1))
list_difference = list(difference_1.union(difference_2))
print(list_difference)
Output:
[50, 5, 40, 25, 60, 15]
For this method, the first step is to store the two lists in two variables. The set()
function is used to convert these lists into sets. In the same step, the set.difference()
function is used to find the difference between the two sets. Note that the difference_1
variable gets the elements in list_1
and not in list_2
.
The difference_2
variable gets the elements that are in list_2
and not in list_1
. After that, the union
function is used to get all the elements from the difference_1
and difference_2
variables. Additionally, the list()
function is used to convert both sets into lists again. Finally, the resultant list is printed.
Use set.symmetric_difference()
to Find the Difference Between Two Lists in Python
Here, the set()
method is used to convert both the lists into sets initially.
The symmetric_difference()
method is used to return the elements that are either in the first set or in the second set. This method does not return the intersection like the common elements of the two sets.
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
set_difference = set(list_1).symmetric_difference(set(list_2))
list_difference = list(set_difference)
print(list_difference)
Output:
[5, 40, 15, 50, 25, 60]
The set()
method is also used to convert lists into sets. After getting the difference, the list()
function is used to get and convert the final set, like converting set_difference
back into a list.
Use the Keyword in
to Find the Difference Between the Two Lists in Python
The in
keyword helps check whether an element is present in an iterable sequence like a list or not.
The append()
function is also used in this method. append()
is used to add elements to an existing list; it does not make a new list, but it modifies the initial list by adding elements to it.
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
list_difference = []
for element in list_1:
if element not in list_2:
list_difference.append(element)
print(list_difference)
Output:
[5, 15, 25]
In this method, we first initialize the final list that has to be printed with zero elements. Then we use a forloop to start the iteration with the first list, which is list_1
. After that, we use the keyword not in
to check if an element is in the second list or not.
Finally, we use the append()
method to add the elements that are not in the second list to the initial list, i.e., list_difference
. This method’s drawback, though, is it doesn’t return the elements from the second list.
To make this method simpler, use the list comprehension. The list comprehension is a method that helps create a new list with the elements of an existing list.
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
list_difference = [element for element in list_1 if element not in list_2]
print(list_difference)
Output:
[5, 15, 25]
Use the NumPy
Library to Find the Difference Between Two Lists in Python
Python’s NumPy
Library is a very commonly used library. This library helps the user perform tasks in arrays, matrices, and linear algebra.
In this method, three functions of the NumPy
library are used to get the difference between two lists. These three functions are np.array()
, np.setdiff1d()
, and np.concatenate()
.
An array is the primary data structure of the NumPy
library. The np.array()
function helps you create an array and assists you in working with them.
The np.setdiff1d()
function helps you find the set difference between two arrays and return the values that are in the first array and not in the second array.
The np.concatenate
function is used to combine two or more NumPy
arrays together. Check the example below:
import numpy as np
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
array_1 = np.array(list_1)
array_2 = np.array(list_2)
difference_1 = np.setdiff1d(array_1, array_2)
difference_2 = np.setdiff1d(array_2, array_1)
list_difference = np.concatenate((difference_1, difference_2))
print(list(list_difference))
Output:
[5, 15, 25, 40, 50, 60]