In this python tuple program, we will find common elements between two lists of tuples.
Steps to solve the program
- Take two lists of tuples as input.
- Create a set of two lists to remove duplicate tuples by using set().
- Find the common tuples between two lists of tuples using the ” & ” operator.
- Print the output.
tup1 = [(1,5),(4,8),(3,9)]
tup2 = [(3,9),(5,6),(1,5),(0,4)]
print(f"Tuples: {tup1} and {tup2}")
result = (set(tup1) & set(tup2))
print("Common elements between tuples: ",result)
Output :
Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples: {(3, 9), (1, 5)}