Check if two list of tuples are identical in Python

In Python, you may need to check if two lists of tuples are identical. A list of tuples is a data structure where each element is a tuple containing multiple values. Python provides several methods to compare such structures.

Using the == Operator

The most straightforward way to check if two lists of tuples are identical is using the == operator. This compares both the content and order of elements ?

list_1 = [(11, 14), (54, 58)]
list_2 = [(98, 0), (10, 13)]

print("First list:", list_1)
print("Second list:", list_2)

result = list_1 == list_2
print("Are the lists identical?", result)
First list: [(11, 14), (54, 58)]
Second list: [(98, 0), (10, 13)]
Are the lists identical? False

Example with Identical Lists

list_1 = [(11, 14), (54, 58)]
list_2 = [(11, 14), (54, 58)]

result = list_1 == list_2
print("Are the lists identical?", result)
Are the lists identical? True

Order-Independent Comparison

If you want to check if two lists contain the same tuples regardless of order, convert them to sets ?

list_1 = [(11, 14), (54, 58)]
list_2 = [(54, 58), (11, 14)]

# Order matters with ==
print("Using == operator:", list_1 == list_2)

# Order doesn't matter with sets
print("Using sets:", set(list_1) == set(list_2))
Using == operator: False
Using sets: True

Finding Common Elements

To find common tuples between two lists, use set intersection ?

list_1 = [(11, 14), (54, 58), (10, 13)]
list_2 = [(98, 0), (10, 13)]

# Using & operator
common_elements = set(list_1) & set(list_2)
print("Common elements using &:", common_elements)

# Using intersection() method
common_elements = set(list_1).intersection(set(list_2))
print("Common elements using intersection():", common_elements)
Common elements using &: {(10, 13)}
Common elements using intersection(): {(10, 13)}

Comparison Methods Summary

Method Order Sensitive? Use Case
== operator Yes Exact equality check
set(list1) == set(list2) No Same elements, any order
set(list1) & set(list2) No Find common elements

Conclusion

Use the == operator for exact comparison including order. Use sets for order-independent comparison or to find common elements between lists of tuples.

Updated on: 2026-03-25T17:26:31+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements