Difference between two lists Python
In this post, you will learn how to find the difference between two lists using the Python programming language.
A list is a sequence of indexed and ordered values like an array. It is mutable, which means we can change the order of elements in a list. A list in Python is a linear data structure that can hold heterogeneous elements. It is flexible to shrink and grow, and there is no need to declare it.
Input:
list1 = [5, 10, 15, 20, 25, 30, 35]
list2 = [10, 15, 30]
Output = list1 - list2
Output:
[5, 20, 25, 35]
There are different ways to find the difference between two lists in Python.
Find the difference between two lists in Python using the in operator
In the given Python program, we are using the loop and in keyword to find the difference between two lists.
list1 = [5, 10, 15, 20, 25, 30, 35, 40]
list2 = [25, 40, 35]
result = []
for element in list1:
if element not in list2:
result.append(element)
print(result)
Output of the above code:
[5, 10, 15, 20, 30]
Find the difference between two lists in Python using the set() method
Python provides a built-in function set() that takes any iterable as an argument and converts it into a set object. Here, we have used this method to find the difference between two lists.
list1 = [5, 10, 15, 20, 25, 30, 35, 40]
list2 = [10, 20, 30]
s = set(list2)
result = [x for x in list1 if x not in s]
print(result)
Output of the above code:
[5, 15, 25, 35, 40]
Find the difference between two lists in Python using function
Here, we create a user-defined function to find the difference between them.
def list_difference(x, y):
list_diff = [i for i in x + y if i not in x or i not in y]
return list_diff
# Driver Code
list1 = [11, 14, 64, 33, 74, 39]
list2 = [11, 64, 74]
result = list_difference(list1, list2)
print(result)
Output of the above code:
[14, 33, 39]
Find the difference between two lists in Python using numpy.concatenate() function
The concatenate() function is a function from the NumPy package. The numpy.concatenate() function concatenate a sequence of arrays along an existing axis.
import numpy as np
list1 = np.array([5, 6, 7, 3, 1, 9, 4])
list2 = np.array([3, 1, 9])
diff1 = np.setdiff1d(list1, list2)
diff2 = np.setdiff1d(list2, list1)
result = np.concatenate((diff1, diff2))
print(list(result))
Output of the above code:
[4, 5, 6, 7]
Find the difference between two lists in Python using symmetric_difference() function
The symmetric_difference() method returns a set that contains all items from both set, but not the items that are present in both sets.
list1 = [5, 13, 53, 77, 45, 85]
list2 = [5, 53, 45]
set_diff = set(list1).symmetric_difference(set(list2))
result = list(set_diff)
print(result)
Output of the above code:
[13, 85, 77]
Related Articles
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module