Open In App

Python Program to Remove duplicate tuples irrespective of order

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of binary tuples, the task is to write a Python program to remove all tuples that are duplicates irrespective of order, i.e delete if contains similar elements, irrespective of order.

Input : test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
Output : [(1, 2), (5, 7), (4, 6), (2, 9)]
Explanation : (2, 1), (6, 4) are removed as (1, 2), (4, 6) are already included.

Input : test_list = [(4, 7), (1, 2), (9, 2), (2, 1), (5, 7), (7, 4), (9, 2)]
Output : [(1, 2), (5, 7), (4, 7), (2, 9)]
Explanation : (2, 1), (7, 4) are removed as (1, 2), (4, 7) are already included.

Method 1 : Using map(), sorted(), set() and list()

In this, we sort each element of tuple using sorted() and map(). Then result is converted to set to remove duplicates. At last, set is converted to list.

Example:

Python3




# initializing list
test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using map() to get all sorted
# set removes duplicates
res = list({*map(tuple, map(sorted, test_list))})
 
# printing result
print("Tuples after removal : " + str(res))


Output

The original list is : [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
Tuples after removal : [(1, 2), (5, 7), (4, 6), (2, 9)]

Time complexity: O(n log n), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list. 

Method 2 : Using list comprehension, sorted() and set()

This is a naive approach for the required solution. In this, each element of list is sorted using list comprehension and sorted(), then result is converted back to remove duplicates using set().

Example:

Python3




# initializing list
test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# sorting tuples
temp = [tuple(sorted(sub)) for sub in test_list]
 
# removing duplicates
res = list(set(temp))
 
# printing result
print("Tuples after removal : " + str(res))


Output

The original list is : [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
Tuples after removal : [(1, 2), (5, 7), (4, 6), (2, 9)]

Method#3: Using Recursive method.

The function remove_duplicates takes in the test_list, an optional index (default value is 0), and a res set (default value is an empty set).
At each recursive call, the function checks if the index has reached the end of the test_list. If it has, then it returns the res set as a list.
If the index has not reached the end of the test_list, then the function adds the sorted tuple at the index to the res set.
The function then calls itself recursively with the index incremented by 1 and the res set passed along.

Python3




def remove_duplicates(test_list, index=0, res=set()):
    if index == len(test_list):
        return list(res)
    res.add(tuple(sorted(test_list[index])))
    return remove_duplicates(test_list, index+1, res)
# initializing list
test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# calling recursive function to remove duplicates
res = remove_duplicates(test_list)
 
# printing result
print("Tuples after removal : " + str(res))


Output

The original list is : [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
Tuples after removal : [(1, 2), (5, 7), (4, 6), (2, 9)]

Time Complexity: O(nlogn), where n is the length of the input list. This is because for each element in the list, we are sorting it before adding it to the set. Sorting takes O(logn) time, and we are doing this n times, so the overall time complexity is O(nlogn).

Auxiliary Space: O(n), where n is the length of the input list. This is because we are creating a set to store the unique tuples, and in the worst case scenario, where all the tuples are unique, the set will contain n elements. Additionally, the recursive function call stack will also take up space proportional to the length of the input list. However, the space complexity of the function call stack is generally not included in the overall space complexity analysis.

METHOD 4:Using re module .

The idea is to use the re-module to remove duplicate tuples irrespective of order. It first converts each tuple to a string by sorting it and then converting it to a string using str(). It then sorts the resulting list of strings. Next, it uses regular expressions to remove duplicates from the sorted list. It converts the sorted list to a set to remove duplicates and then converts it back to a list. Finally, it converts each string in the unique list back to a tuple using eval(), which evaluates the string as a Python expression.

Steps:

  1. Define the original list of tuples
  2. Convert each tuple to a string by sorting it and then converting it to a string using str()
  3. Sort the resulting list of strings
  4. Use regular expressions to remove duplicates from the sorted list
  5. Convert the sorted list to a set to remove duplicates and then convert it back to a list
  6. Convert each string in the unique list back to a tuple using eval()
  7. Print the original list and the unique list of tuples

Python3




import re
 
original_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
 
# Convert tuples to strings and
# sort the list
sorted_list = sorted([str(sorted(t)) for t in original_list])
 
# Use regular expressions to
# remove duplicates
unique_list = list(set(sorted_list))
unique_list.sort()
 
# Convert strings back to tuples
unique_list = [tuple(eval(t)) for t in unique_list]
 
print("Original list: ", original_list)
print("Tuples after removal: ", unique_list)


Output

Original list:  [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]
Tuples after removal:  [(1, 2), (2, 9), (4, 6), (5, 7)]

Time Complexity: O(N*log N)
Space Complexity: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads