I have two list of tuples,A and B, that stores the pairs of data ids, and I want to remove pairs from A if the pair (x,y) or (y,x) in B are also in A.
I tried to do it by using for loop like below,
A = [(321, 33), (56, 991), (645, 2), (8876, 556)] B = [(33, 321), (645, 2)] for pair in B: if pair in A: A.remove(pair) elif (pair[1], pair[0]) in A: A.remove((pair[1], pair[0])) print(A) # [(56, 991), (8876, 556)] but when the elements in list is large, this code runs very slowly.
So I want to make this code faster, possibly avoid using for loops or completely different way. Can someone help me to solve this problem?