Python | Check if two lists are identical
This article deals with the task of ways to check if two unordered list contains exact similar elements in exact similar position, i.e to check if two lists are exactly equal. This is quite a useful utility and can be used in day-day programming.
Method 1: Using list.sort() and == operator sort() coupled with == operator can achieve this task. We first sort the list, so that if both the lists are identical, then they have elements at the same position. But this doesn’t take into account the ordering of elements in list.
Python3
# Python 3 code to demonstrate # check if list are identical # using sort() + == operator # initializing lists test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # printing lists print ( "The first list is : " + str (test_list1)) print ( "The second list is : " + str (test_list2)) # sorting both the lists test_list1.sort() test_list2.sort() # using == to check if # lists are equal if test_list1 = = test_list2: print ( "The lists are identical" ) else : print ( "The lists are not identical" ) |
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method 2: Using collections.Counter() Using Counter(), we usually are able to get the frequency of each element in a list, checking for it, for both lists, we can check if two lists are identical or not. But this method also ignores the ordering of the elements in the list and only takes into account the frequency of elements.
Python3
# Python 3 code to demonstrate # check if list are identical # using collections.Counter() import collections # initializing lists test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # printing lists print ( "The first list is : " + str (test_list1)) print ( "The second list is : " + str (test_list2)) # using collections.Counter() to check if # lists are equal if collections.Counter(test_list1) = = collections.Counter(test_list2): print ( "The lists are identical" ) else : print ( "The lists are not identical" ) |
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Time Complexity: O(n)
Auxiliary Space : O(n)
Method 3: Using sum() + zip() + len() Using sum() + zip(), we can get sum of one of the list as summation of 1 if both the index in two lists have equal elements, and then compare that number with size of other list. This also requires first to check if two lists are equal before this computation. It also checks for the order.
Python3
# Python 3 code to demonstrate # check if list are identical # using sum() + zip() + len() # initializing lists test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # printing lists print ( "The first list is : " + str (test_list1)) print ( "The second list is : " + str (test_list2)) # using sum() + zip() + len() to check if # lists are equal if len (test_list1) = = len (test_list2) and len (test_list1) = = sum ([ 1 for i, j in zip (test_list1, test_list2) if i = = j]): print ( "The lists are identical" ) else : print ( "The lists are not identical" ) |
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Time Complexity: O(n)
Space Complexity: O(n)
Method 4 : Using reduce() + map() Carefully coupling power of map() to hash values and utility of reduce(), we can achieve this task of checking for equality of two lists to be identical. This also takes into account the ordering of the list.
Python3
# Python 3 code to demonstrate # check if list are identical # using map() + reduce() import functools # initializing lists test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # printing lists print ( "The first list is : " + str (test_list1)) print ( "The second list is : " + str (test_list2)) # using map() + reduce() to check if # lists are equal if functools. reduce ( lambda i, j: i and j, map ( lambda m, k: m = = k, test_list1, test_list2), True ): print ( "The lists are identical" ) else : print ( "The lists are not identical" ) |
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using all() and zip()
Here is an example of using the all() function and zip() to check if two lists are identical:
Python3
# Initialize the lists test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # Print the lists print ( "List 1:" , test_list1) print ( "List 2:" , test_list2) # Check if the lists are identical result = all (x = = y for x, y in zip (test_list1, test_list2)) # Print the result print ( "The lists are identical:" , result) #This code is contributed by Deepthu |
List 1: [1, 2, 4, 3, 5] List 2: [1, 2, 4, 3, 5] The lists are identical: True
In the above code, we are using the all() function to check if all elements in the result of zipping the two lists are equal. The all() function returns True if all elements in an iterable are True, and False otherwise.
The zip() function in Python takes iterables (such as lists, tuples, or strings) as input and returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the input iterables.
We use a generator expression (x == y for x, y in zip(test_list1, test_list2)) to iterate through the zipped lists and compare the elements at each index. If all elements are equal, the generator expression returns a list of all True values, and the all() function returns True. If any element is not equal, the generator expression returns a list with at least one False value, and the all() function returns False.
Finally, we print the result of the all() function using the print() function. If the result is True, it means that the lists are identical. If the result is False, it means that the lists are not identical.
The time complexity of this approach is O(n), as it involves iterating through the lists once and performing constant-time operations on each element. The space complexity is also O(1), as it only involves a few constant-size variables.
Method 6: Using operator.countOf() method
Python3
# Python 3 code to demonstrate # check if list are identical import operator as op def checkIdentical(test_list1, test_list2): for i in test_list1: if op.countOf(test_list1, i) ! = op.countOf(test_list2, i): return False if ( len (test_list1) ! = len (test_list2)): return False return True # initializing lists test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # printing lists print ( "The first list is : " + str (test_list1)) print ( "The second list is : " + str (test_list2)) if checkIdentical(test_list1, test_list2): print ( "The lists are identical" ) else : print ( "The lists are not identical" ) |
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 7: Using counter():
Python3
from collections import Counter test_list1 = [ 1 , 2 , 4 , 3 , 5 ] test_list2 = [ 1 , 2 , 4 , 3 , 5 ] # printing lists print ( "The first list is : " + str (test_list1)) print ( "The second list is : " + str (test_list2)) if Counter(test_list1) = = Counter(test_list2): print ( "The lists are identical" ) else : print ( "The lists are not identical" ) #This code is contributed by Jyothi pinjala. |
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Time Complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...