The Wayback Machine - https://web.archive.org/web/20250116022815/https://www.geeksforgeeks.org/python-check-if-two-lists-are-identical/
Open In App

Check if two lists are identical in Python

Last Updated : 24 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article we will explore various methods to check if two lists are identical or not. By “identical”, we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==).

Using Equality Operator (==)

The easiest way to check if two lists are identical is by using the equality operator (==). This method compares each element of both lists in the same order and return True if they match, otherwise False.

Python
a = [1, 2, 3]
b = [1, 2, 4]

# Using Equality Operator
res = a == b
print(res)

Output
False

Explanation: Since the last elements in a and b are different (3 vs 4), so the result is False.

Let’s explore different methods to check if two lists are identical:

Using a loop

In this approach, we first check if lists are of equal length. Then, we use a for loop to compare each element. If any element differs or the lengths do not match then the lists would not be identical. Otherwise, identical.

Python
a = [1, 2, 3]
b = [1, 2, 3]

# Set a flag to True to assume lists are identical initially
flag = True

# Check if lengths of both lists are equal
if len(a) != len(b):
  
     # If lengths differ, set flag to False
    flag = False 
else:
  
    # Iterate over each element in lists
    for i in range(len(a)):   
      
        # Compare corresponding elements of both lists
        if a[i] != b[i]:
              
            # If any elements differ, set flag to False
            flag = False  
            
            # Exit the loop since we found a difference
            break  

print(flag)

Output
True

Using all() with zip()

Another effective way to check for identical lists is by using the all() function along with zip(). This method pairs elements from both lists and checks if all corresponding elements are equal. If any pair differs, all() returns False (means, lists are not identical).

Python
a = [5, 6, 7]
b = [5, 6, 7]

# Check if all elements match
flag = all(x == y for x, y in zip(a, b))
print(flag)

Output
True

Explanation: The zip() function pairs each element from a and b and all() checks whether each pair matches. Since all pairs are identical then output is True.

Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg