Python – Remove Duplicate subset Tuples

• Last Updated : 03 Jul, 2020

Sometimes, while working with Python tuples, we can have a problem in which we need to perform removal of tuples, which are already present as subsets in other tuples. This kind of problem can be use ful in data preprocessing. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7, 10), (6, 7), (6, 9), (15, 34)], K = 2
Output : [(6, 9, 17, 18), (15, 34, 56), (6, 7, 10)]

Input : test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7, 10)], K = 2
Output : [(6, 9, 17, 18), (15, 34, 56), (6, 7, 10)]

Method #1 : Using setdefault() + list comprehension
This is one of the way in which this task can be solved. In this, we perform the task of initializing the list and keeping elements to compare. At last, list comprehension is used to perform removal of subset tuples. This method gives flexibility of size of tuples for removal.

 # Python3 code to demonstrate working of # Remove Duplicate subset Tuples# Using setdefault() + list comprehension  # initializing liststest_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]  # printing original listprint("The original list is : " + str(test_list))  # initializing K K = 2  # Remove Duplicate subset Tuples# Using setdefault() + list comprehensiontemp = {}for sub in test_list:    temp2 = sub[:K]    temp.setdefault(temp2, []).append(sub)res = [sub for sub in test_list if len(sub) > K or len(temp[sub]) == 1]  # printing result print("Tuple list after removal : " + str(res))
Output :
The original list is : [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
Tuple list after removal : [(6, 9, 17, 18), (15, 34, 56), (6, 7)]

Method #2 : Using all() + any() + loop
The combination of above functions provide yet another way to solve this problem. In this, we test for all subsets, irrespective of sizes. The any() function is used to check if any of the tuple is new in all elements of particular tuple extracted using all().

 # Python3 code to demonstrate working of # Remove Duplicate subset Tuples# Using all() + any()+ loop  # initializing liststest_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]  # printing original listprint("The original list is : " + str(test_list))  # Remove Duplicate subset Tuples# Using all() + any() + loopres = []test_list = sorted(test_list, key = lambda x: len(x))for idx, sub in enumerate(test_list):    if any(all(ele in sub2 for ele in sub) for sub2 in test_list[idx + 1:]):        pass     else:        res.append(sub)  # printing result print("Tuple list after removal : " + str(res))
Output :
The original list is : [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
Tuple list after removal : [(6, 9, 17, 18), (15, 34, 56), (6, 7)]

My Personal Notes arrow_drop_up