Example:
tuple = ("python", "includehelp", 43, 54.23)
Tuple Intersection
The intersection of tuples is finding common elements from both the tuples i.e. the intersection tuple consists of all elements that are common in both the tuples.
In this program, we are given two tuples. We need to create a python program to return a tuple that contains the intersection elements i.e. elements that are common in both tuples.
Input:
tuple1 = (4, 1, 7, 9, 3, 5) , tuple2 = (2, 4, 6, 7, 8)
Output:
(4, 7)
Method 1:
One method to perform tuple intersection is by performing set intersection operation "&" which will find the intersection of sets. And we will be performing interconversion from a tuple to set and set to a tuple using set() and tuple() methods.
Output:
Method 2:
Another method to solve the problem is by using the intersection() method in place of & operator on sets. In this program also the interconversion is done using the set() method and the tuple() method.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer