I would definitely commend @Nick_Boyts suggestion in the first instance, paired with Group by Key. But to answer your secondary question, if you wanted it in Python something like this? You get the elements outputted into the four groups, so you just select the index of the group(s) you are interested in
a = IN[0][0]
b = IN[0][1]
elems = IN[1]output = [,,,]
for i,j,el in zip(a,b,elems):
if i == True and j == True:
n = 0
elif i == True and j == False:
n = 1
elif i == False and j == True:
n = 2
elif i == False and j == False:
n = 3
output[n].append(el)Assign your output to the OUT variable.
OUT = output