Open In App

Python | Check if two lists are identical

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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")


Output

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")


Output

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")


Output

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")


Output

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


Output

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")


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(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.


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)



Previous Article
Next Article

Similar Reads

Python | Check whether two lists are circularly identical
Given two lists, check if they are circularly identical or not. Examples: Input : list1 = [10, 10, 0, 0, 10] list2 = [10, 10, 10, 0, 0] Output : Yes Explanation: yes they are circularly identical as when we write the list1 last index to second last index, then we find it is circularly same with list1 Input : list1 = [10, 10, 10, 0, 0] list2 = [1, 1
5 min read
Python Program To Check If Two Linked Lists Are Identical
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method
4 min read
Python List Equality | Program to check if two given matrices are identical
We are given two square matrices of same order. Check if two given matrices are identical. Examples: Input : A = [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] B = [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] Output: Matrices are identical We have existing solution for this problem please refer C Program to check if two give
3 min read
Python | Check if two list of tuples are identical
Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is the simplest and elegant way to perform this task. I
4 min read
Check if two PDF documents are identical with Python
Python is an interpreted and general purpose programming language. It is a Object-Oriented and Procedural paradigms programming language. There are various types of modules imported in python such as difflib, hashlib. Modules used:difflib : It is a module that contains function that allows to compare set of data.SequenceMatcher : It is used to comp
2 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the first element is identical to all other elements in t
5 min read
Python | Check if tuple and list are identical
Sometimes while working with different data in Python, we can have a problem of having data in different containers. In this situations, there can be need to test if data is identical cross containers. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is the brute force method to perform this particular tas
7 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Python - Convert Lists into Similar key value lists
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corresponding list, are mapped to 6. Input : test_list1
12 min read
Practice Tags :