Check value existence in two lists and gather the result in a csv
General problem

It's not much, but for your general problem, mathematically speaking, I'd split the logic into three steps (It might be a bit more code, but personally speaking, it keeps things cleaner):




I'd intersect list_A with list_B:

def intersect(list_A, list_B):
    return [[x, True, True] for x in set(list_A).intersection(list_B)]


Which is going to give you:


  [[3, True, True], [4, True, True]]

Then, I'd do list_A - list_B:

def a_diff_b(list_A, list_B):
    return [[x, True, False] for x in set(list_A).difference(list_B)]


Which is going to give you:


  [[1, True, False], [2, True, False]]

Then, I'd do list_B - list_A:

def b_diff_a(list_A, list_B):
    return [[x, False, True] for x in set(list_B).difference(list_A)]


Which is going to give you:


  [[5, False, True]]





Intersection


  In mathematics, the intersection A ∩ B of two sets A and B is the set
  that contains all elements of A that also belong to B (or
  equivalently, all elements of B that also belong to A), but no other
  elements.


Complement


  In set theory, the complement of a set A refers to elements not in A.
  The relative complement of A with respect to a set B, also termed the
  difference of sets A and B, written B ∖ A, is the set of elements in B
  but not in A. When all sets under consideration are considered to be
  subsets of a given set U, the absolute complement of A is the set of
  elements in U but not in A.