Python | Remove matching tuples

The problem of removing the matching elements from two lists and constructing a new list having just the filtered elements not present in 2nd list has been discussed earlier, but sometimes, we have more than an elementary element, but a tuple as element of list. Handling such a case requires different type of handling. Lets discuss certain ways how this problem can be solved.

Method #1 : Using list comprehension
This particular task can be done using the list comprehension as shorthand for the for loop which we would have used. We just check for the existence of one tuple in another and make decision accordingly.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# filter repeated tuple
# using list comprehension
  
# initializing lists 
test_list1 = [('Geeks', 'for'), ('Geeks', 'is'), ('Computer', 'Science')]
test_list2 = [('Geeks', 'for'), ('Geeks', 'is')]
  
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 1 : " + str(test_list2))
  
# using list comprehension
# filter repeated tuple
res =  [sub for sub in test_list1 if sub not in test_list2]
  
# print result
print("The filtered list of tuples : " + str(res))

chevron_right


Output :

The original list 1 : [('Geeks', 'for'), ('Geeks', 'is'), ('Computer', 'Science')]
The original list 1 : [('Geeks', 'for'), ('Geeks', 'is')]
The filtered list of tuples : [('Computer', 'Science')]

 

Method #2 : Using set() + “-” operator
The task of getting the difference of two lists can also be done using the set that converts the list and then minus operator can be used to get the set difference.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# filter repeated tuple
# using set() + "-" operator
  
# initializing lists 
test_list1 = [('Geeks', 'for'), ('Geeks', 'is'), ('Computer', 'Science')]
test_list2 = [('Geeks', 'for'), ('Geeks', 'is')]
  
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 1 : " + str(test_list2))
  
# using set() + "-" operator
# filter repeated tuple
res = list(set(test_list1) - set(test_list2))
  
# print result
print("The filtered list of tuples : " + str(res))

chevron_right


Output :

The original list 1 : [('Geeks', 'for'), ('Geeks', 'is'), ('Computer', 'Science')]
The original list 1 : [('Geeks', 'for'), ('Geeks', 'is')]
The filtered list of tuples : [('Computer', 'Science')]


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.