I have two list of tuples
a = [('head1','a'),('head2','b'),('head3','x'),('head4','z')]
b = [('head5','u'),('head6','w'),('head7','x'),('head8','y'),('head9','z')]
I want to take the intersection of 2nd element of each tuple for example set {a[0][0],a[0][1],a[0][2],a[0][3]}
intersection with set {b[0][0],b[0][1],b[0][2],b[0][3],b[0][4]}
from list a
and b
such that it returns the first element mapping of the tuple if intersection value exist. The resulting output should be like following:
res = [('head3','head7'),('head4','head9')]
So far I have tried this:
x = [(a[i][0],b[j][0]) for i in range(len(a)) for j in range(len(b)) if a[0][i] == b[0][j]]
but got an error IndexError: tuple index out of range
What could be the correct and fastest way of doing this ?