Example:
tuple = ("python", "includehelp", 43, 54.23)
Performing the union of Tuple
In this program, we have two tuples with integer elements. Our task is to create a python program to perform the union operation between two tuples.
Input:
tup1 = (3, 7, 1, 9), tup2 = (4, 5, 7, 1)
Output:
(1, 3, 4, 5, 7, 9)
Method 1:
One way to solve the problem is by using the + operator to perform union operations. The method is performed over sets so we will perform the conversion using set() and tuple() methods.
Output:
Method 2:
Another method to perform the union operation is by using the union() method. This method works with sets, so we will be converting the tuples to set using the set() method and then converting the union set back to tuples using the tuple() method.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer