Python – Remove Duplicate subset Tuples

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove Duplicate subset Tuples
# Using setdefault() + list comprehension
  
# initializing lists
test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 2
  
# Remove Duplicate subset Tuples
# Using setdefault() + list comprehension
temp = {}
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)) 

chevron_right


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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove Duplicate subset Tuples
# Using all() + any()+ loop
  
# initializing lists
test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Remove Duplicate subset Tuples
# Using all() + any() + loop
res = []
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)) 

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.