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.
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:
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.
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.
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.
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.
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 :)