In this article, we will understand the different ways to compare two lists in Python. We often come across situations wherein we need to compare the values of the data items stored in any structure say list, tuple, string, etc.
Comparison
is the method of checking the data items of a list against equality with the data items of another list.
Methods to Compare Two Lists in Python
We can use either of the following methods to perform our comparison:
- The reduce() and map() function
- The collection.counter() function
- Python sort() function along with == operator
- Python set() function along with == operator
- The difference() function
1. Python reduce() and map() functions
We can use the Python map() function along with functools.reduce() function to compare the data items of two lists.
The map()
method accepts a function and an iterable such as list, tuple, string, etc. as arguments.
It applies the passed function to each item of the iterable and then returns a map object i.e. an iterator as the result.
The functools.reduce()
method applies the passed function to every element of the input iterable in a recursive manner.
Initially, it would apply the function on the first and the second element and returns the result. The same process will continue on each of the elements until the list has no elements left.
As a combination, the map() function would apply the input function to every element and the reduce() function will make sure that it applies the function in a consecutive manner.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import functools l1 = [10, 20, 30, 40, 50] l2 = [10, 20, 30, 50, 40, 70] l3 = [10, 20, 30, 40, 50] if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l2), True): print ("The lists l1 and l2 are the same") else: print ("The lists l1 and l2 are not the same") if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l3), True): print ("The lists l1 and l3 are the same") else: print ("The lists l1 and l3 are not the same") |
Output:
1 2 3 |
<span style="color: #008000;"><strong>The lists l1 and l2 are not the same The lists l1 and l3 are the same </strong></span> |
2. Python collection.counter() method
The collection.counter() method can be used to compare lists efficiently. The counter() function counts the frequency of the items in a list and stores the data as a dictionary in the format <value>:<frequency>.
If two lists have the exact same dictionary output, we can infer that the lists are the same.
Note: The list order has no effect on the counter() method.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import collections l1 = [10, 20, 30, 40, 50] l2 = [10, 20, 30, 50, 40, 70] l3 = [10, 20, 30, 40, 50] if collections.Counter(l1) == collections.Counter(l2): print ("The lists l1 and l2 are the same") else: print ("The lists l1 and l2 are not the same") if collections.Counter(l1) == collections.Counter(l3): print ("The lists l1 and l3 are the same") else: print ("The lists l1 and l3 are not the same") |
Output:
1 2 3 |
<span style="color: #008000;"><strong>The lists l1 and l2 are not the same The lists l1 and l3 are the same </strong></span> |
3. Python sort() method and == operator to compare lists
We can club the Python sort() method with the == operator to compare two lists.
Python sort()
method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.
Note: The order of the list does not affect this method because we’ll be sorting the lists before comparison.
Further, the == operator
is used to compare the list, element by element.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import collections l1 = [10, 20, 30, 40, 50] l2 = [10, 20, 30, 50, 40, 70] l3 = [50, 10, 30, 20, 40] l1.sort() l2.sort() l3.sort() if l1 == l3: print ("The lists l1 and l3 are the same") else: print ("The lists l1 and l3 are not the same") if l1 == l2: print ("The lists l1 and l2 are the same") else: print ("The lists l1 and l2 are not the same") |
Output:
1 2 3 |
<span style="color: #008000;"><strong>The lists l1 and l3 are the same The lists l1 and l2 are not the same </strong></span> |
4. Python set() method and == operator to compare two lists
Python set() method manipulates the data items of an iterable to a sorted sequence set of data items without taking the order of elements into consideration.
Further, the == operator
is used for comparison of the data items of the list in an element-wise fashion.
Example:
1 2 3 4 5 6 7 8 |
l1 = [10, 20, 30, 40, 50] l3 = [50, 10, 30, 20, 40] a = set(l1) b = set(l3) if a == b: print("Lists l1 and l3 are equal") else: print("Lists l1 and l3 are not equal") |
Output:
1 2 |
<span style="color: #008000;"><strong>Lists l1 and l3 are equal </strong></span> |
5. Python Custom List Comprehension to Compare Two Lists
We can use Python List comprehension to compare two lists.
Example:
1 2 3 4 5 6 7 8 |
l1 = [10, 20, 30, 40, 50] l3 = [50, 75, 30, 20, 40, 69] res = [x for x in l1 + l3 if x not in l1 or x not in l3] print(res) if not res: print("Lists l1 and l3 are equal") else: print("Lists l1 and l3 are not equal") |
In the above code, we set a pointer element ‘x’ to the list l1 and l3. Further, we check if the element pointed by the pointer element is present in the lists.
Output:
1 2 3 |
<span style="color: #008000;"><strong>[10, 75, 69] Lists l1 and l3 are not equal </strong></span> |
Conclusion
Thus, in this article, we have covered and understood a number of ways to compare multiple lists in Python.