Write a Python program to Intersection in Tuple Records Data


The program is written in Python and performs the following operations:

  • Defines two lists of tuples called "l1" and "l2".
  • Prints the two lists of tuples.
  • Defines a new list "res" by using a list comprehension, which is a concise way to create a list in Python.
  • The list comprehension iterates through each tuple in "l1" and for each tuple, it iterates through each tuple in "l2".
  • The condition "if i == j" checks if the current tuple in "l1" is equal to the current tuple in "l2".
  • If the condition is true, the tuple is added to the list "res".
  • Prints the resulting list after the intersection.

The final output of the program will be the common tuples (if any) between the two lists "l1" and "l2". The list comprehension will return the common tuples, if any, in the two lists, by checking if each tuple in "l1" is equal to each tuple in "l2".

Source Code

l1 = [('A' , 65), ('D' , 68), ('B' , 66)]
l2 = [('D' , 68), ('C' , 67), ('A' , 65)]
 
print("list 2 : " , l2)
print("list 1 : " , l1)
 
res = [i for i in l1
	for j in l2 if i == j]
 
print("Intersection of data records : ",res)

Output

list 2 :  [('D', 68), ('C', 67), ('A', 65)]
list 1 :  [('A', 65), ('D', 68), ('B', 66)]

Intersection of data records :  [('A', 65), ('D', 68)]

Example Programs