Lists are one of the most important data structures in Python programming. And one of the important operations on the list is to find the difference between two lists in python. We will learn about different methods to compare lists with examples and code.
What is Python List Difference?
Finding things that are present in one list but absent from the other is known as Python list difference. But remember that the comparison ordering matters. We also need to keep in mind whether we want repetition in the list or not, because the list can have duplicate values.
Before moving forward, let's understand two types of differences:
- Asymmetric difference: It returns the items that list1 has but list2 does not. This means if list2 has an item that is not found in list1, it does not count.
- Symmetric difference: It returns the items in list1 that are not in list2 and vice versa.
Let's now look at a few approaches to find differences between the two lists:
1) Using the Membership Function
In python, as we know we have membership operators ‘in’ and ‘not in’, We can use these operators to differentiate between the elements of a list by passing each list in for loop and implementing an if-else condition on it.
It returns an asymmetric difference between the list that is if given two lists: list1 and list2; your function will return the elements that are in list1 but not in list2. But do not return the elements that are in list2 but not in list1 for that you need to reverse the function and find the output.
You can also use the set() function to remove duplicates from a list and then iterate it using a membership operator.
Example:
# Intializing the lists List1 = [4,28,6,99,100,25,18,10,40,73] list2 = [25, 40,73,4,80,63,99,6] new_list1 = [] #empty list for i in List1: if i not in list2: new_list1.append(i) print("elements of list 1 that are not present in list 2 ",new_list1) new_list2 = [] #empty list for i in list2: if i not in List1: new_list2.append(i) print("elements of list 2 that are not present in list 1 ",new_list2)
Output:
elements of list 1 that are not present in list 2 [28, 100, 18, 10] elements of list 2 that are not present in list 1 [80, 63]
We can also cut down our coding lines and make our program easier by using list comprehension instead of loops.
You can more clearly understand what's happening in the above code with the following image:
Here is the new code with list comprehension:
# Intializing the lists list1 = [4,28,6,99,100,25,18,10,40,73] list2 = [25, 40,73,4,80,63,99,6] s = set(list2) # set() function to remove duplicacy new_list1 = [x for x in list1 if x not in s] # List comprehension print("elements of list 1 that are not present in list 2 ",new_list1) s2=set(list1) new_list2 = [x for x in list2 if x not in s2] print("elements of list 2 that are not present in list 1 ",new_list2)
Output:
elements of list 1 that are not present in list 2 [28, 100, 18, 10] elements of list 2 that are not present in list 1 [80, 63]
2) Without using the set() function
In this approach, we don’t use any pre-built function like set(). We simply concatenate a given two lists and iterate a loop over it to check its element's presence in both lists individually and all common elements are removed.
Hence, storing all distinct elements of both lists in a new one. It is a symmetric difference approach to the solution.
Example:
# Python code to get difference of two lists # Not using set() def Diff(list1, list2): li_dif = [i for i in list1 + list2 if i not in list1 or i not in list2] return li_dif # Driver Code list1 = [4,28,6,99,100,25,18,10,40,73] list2 = [25, 40,73,4,80,63,99,6] list3 = Diff(list1, list2) print("All different elements of two given list",list3)
Output:
All different elements of two given list [28, 100, 18, 10, 80, 63]
3) Using Numpy functions
NumPy is an open-source Python library that’s used in almost every field of science and engineering. It is used for working with multidimensional and single-dimensional array elements. It also has functions for working in the domain of linear algebra, Fourier transform, and matrices. The array object in NumPy is called ndarray.
We have so many in-built functions and out of these all functions we have concatenate(arr1,arr2) which merges two lists, and setdiff1d(arr1,arr2,assume_unique=False) which helps us to differentiate between the list i.e. It returns the ndarray which stores the unique values of arr1 which are not in arr2.
Example:
import numpy as np list1 = [4,28,6,99,100,25,18,10,40,73] list2 = [25, 40,73,4,80,63,99,6] dif_1_2 = np.setdiff1d(list1, list2) dif_2_1 = np.setdiff1d(list2, list1) new_list = [np.concatenate((dif_1_2, dif_2_1))] print("All different elements of two given list",new_list)
Output:
All different elements of two given list [array([ 10, 18, 28, 100, 63, 80])]
4) Using symmetric_difference()
The elements that are either in the first set or the second set are returned using the symmetric_difference() technique. Unlike the shared items of the two sets, the intersection is not returned by this technique.
Code:
list1 = [4,28,6,99,100,25,18,10,40,73] list2 = [25, 40,73,4,80,63,99,6] set_dif = [set(list1).symmetric_difference(set(list2))] print(set_dif)
Output:
[{100, 10, 80, 18, 28, 63}]
5) Basic method
In this approach, we find the set of each list and differentiate it from another set. This is the easiest method as it does not require any pre-built function use. First, we convert the list into a set. Second, we will subtract the sets from one another to find the difference. Last, we convert the result back to a list.
Code:
list1 = [4,28,6,99,100,25,18,10,40,73] list2 = [25, 40,73,4,80,63,99,6] new_list1=set(list1)-set(list2) new_list2=set(list2)-set(list1) print("set of elements of list 1 that are not present in list 2 ",new_list1) print("set of elements of list 2 that are not present in list 1 ",new_list2)
Output:
set of elements of list 1 that are not present in list 2 {10, 18, 100, 28} set of elements of list 2 that are not present in list 1 {80, 63}
Conclusion
Today, we learned how to find the difference between two lists in python. If you still have any queries, we can connect you with our expert for python assignment help online anytime. Happy learning :)