Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets []
Example:
list = [3 ,1, 5, 7]
Here, we are given two lists consisting of tuple elements. And we need to perform intersection in the list i.e., find the tuple from which is present in both the list in Python.
Input:
tupList1 = [(8, 1), (5, 10)], tupList2 = [(6, 3), (10, 5)]
Output:
{(5, 10)}
To solve this problem, we will be traversing both lists to tuples to find all the tuples which are common in both. For checking for common tuples, we will not consider the order of elements in the tuple.
Method 1:
We will sort the tuples (we need to find common tuples irrespective of order). Then perform intersection operations on lists using & operator.
Output:
Method 2:
Another way to solve the problem is by converting the tuples to sets (which are ordered) and then do the intersection of these sets using frozenset and map it to get the resultant.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer