Python | Find Dissimilar Elements in Tuples

Sometimes, while working with tuples, we can have a problem in which we need dissimilar features of two records. This type of application can come in Data Science domain. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using set() + "^" operator

This task can be performed using symmetric difference functionality offered by XOR operator over sets. The conversion to set is done by set().



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Dissimilar elements in tuples
# Using set() + "^" operator
  
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
  
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
  
# Dissimilar elements in tuples
# Using set() + "^" operator
res = tuple(set(test_tup1) ^ set(test_tup2))
  
# printing result
print("The Dissimilar elements from tuples are : " + str(res))

chevron_right


Output :

The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)

 

Method #2 : Using symmetric_difference() + set()

This is method similar to above method, the difference is that instead of XOR operator, we use inbuilt function to perform the task of filtering dissimilar elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Dissimilar elements in tuples
# Using symmetric_difference() + set()
  
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
  
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
  
# Dissimilar elements in tuples
# Using symmetric_difference() + set()
res = tuple(set(test_tup1).symmetric_difference(set(test_tup2)))
  
# printing result
print("The Dissimilar elements from tuples are : " + str(res))

chevron_right


Output :

The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.