Python – Union of Tuples
Last Updated :
30 Jan, 2020
Sometimes, while working with tuples, we can have a problem in which we need union 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 union functionality offered by + operator over sets. The conversion to set is done by set().
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1 + test_tup2))
print ( "The union elements from tuples are : " + str (res))
|
Output :
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The union elements from tuples are : (3, 4, 5, 6, 7, 10)
Method #2 : Using union() + set()
This is method similar to above method, the difference is that instead of + operator, we use inbuilt function to perform the task of filtering dissimilar elements.
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1).union( set (test_tup2)))
print ( "The union elements from tuples are : " + str (res))
|
Output :
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The union elements from tuples are : (3, 4, 5, 6, 7, 10)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...