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 == operatorsort() 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.
# 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 listsprint ("The first list is : " + str(test_list1))print ("The second list is : " + str(test_list2)) # sorting both the liststest_list1.sort()test_list2.sort() # using == to check if # lists are equalif test_list1 == test_list2: print ("The lists are identical")else : print ("The lists are not identical") |
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Method 2 : Using collections.Counter()
Using Counter(), we usually are able to get frequency of each element in list, checking for it, for both the list, 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.
# 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 listsprint ("The first list is : " + str(test_list1))print ("The second list is : " + str(test_list2)) # using collections.Counter() to check if # lists are equalif collections.Counter(test_list1) == collections.Counter(test_list2): print ("The lists are identical")else : print ("The lists are not identical") |
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
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.
# 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 listsprint ("The first list is : " + str(test_list1))print ("The second list is : " + str(test_list2)) # using sum() + zip() + len() to check if # lists are equalif 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") |
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
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.
# 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 listsprint ("The first list is : " + str(test_list1))print ("The second list is : " + str(test_list2)) # using map() + reduce() to check if # lists are equalif 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") |
Output :
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


