def reference(alist1,alist2): counter = 0 for x in lis: if x in ref: ref.pop(ref.index(x)) counter += 1 return counter this works I think for your examples, but you should check it against them and other cases. good luck