Remove duplicate tuples from list of tuples in Python

When working with lists of tuples, you often need to remove duplicates to clean your data. Python provides several methods to accomplish this task efficiently.

The any() method checks if any value in an iterable is True. If at least one value is True, it returns True; otherwise, it returns False.

The enumerate() method adds a counter to an iterable and returns it as an enumerate object, which is useful for getting both index and value during iteration.

Method 1: Using set() (Most Efficient)

The simplest approach is to convert the list to a set and back to a list ?

tuples_list = [(23, 45), (25, 17), (35, 67), (25, 17), (23, 45)]

print("Original list:")
print(tuples_list)

# Remove duplicates using set
unique_tuples = list(set(tuples_list))
print("\nAfter removing duplicates:")
print(unique_tuples)
Original list:
[(23, 45), (25, 17), (35, 67), (25, 17), (23, 45)]

After removing duplicates:
[(35, 67), (23, 45), (25, 17)]

Method 2: Using enumerate() and any()

This method preserves the original order of elements ?

def remove_duplicates(tuples_list):
    result = []
    for i, current_tuple in enumerate(tuples_list):
        # Check if current tuple appears in the remaining list
        if not any(current_tuple == other_tuple for other_tuple in tuples_list[:i]):
            result.append(current_tuple)
    return result

tuples_list = [(23, 45), (25, 17), (35, 67), (25, 17), (23, 45)]

print("Original list:")
print(tuples_list)

print("\nAfter removing duplicates (order preserved):")
print(remove_duplicates(tuples_list))
Original list:
[(23, 45), (25, 17), (35, 67), (25, 17), (23, 45)]

After removing duplicates (order preserved):
[(23, 45), (25, 17), (35, 67)]

Method 3: Using dict.fromkeys()

This method also preserves order and is more efficient than Method 2 ?

tuples_list = [(23, 45), (25, 17), (35, 67), (25, 17), (23, 45)]

print("Original list:")
print(tuples_list)

# Remove duplicates while preserving order
unique_tuples = list(dict.fromkeys(tuples_list))
print("\nAfter removing duplicates (order preserved):")
print(unique_tuples)
Original list:
[(23, 45), (25, 17), (35, 67), (25, 17), (23, 45)]

After removing duplicates (order preserved):
[(23, 45), (25, 17), (35, 67)]

Comparison

Method Preserves Order? Time Complexity Best For
set() No O(n) When order doesn't matter
enumerate() + any() Yes O(n²) Understanding the logic
dict.fromkeys() Yes O(n) Order preservation needed

Conclusion

Use set() for fastest duplicate removal when order doesn't matter. Use dict.fromkeys() when you need to preserve the original order efficiently.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements