title: Conditional Cartesian product of a X lists, resulting in dict with k,v pairs?
body: <p>I have a number of lists as follows:</p>
  
  <pre><code>LISTA = ['A1', 'A2']
  LISTB = ['B1', 'B2']
  LISTC = ['C1']
  LISTD = ['D1', 'D2']
  LISTE = ['E1', 'E2', 'E3']
  </code></pre>
  
  <p>I'd like to write a function to get the Cartesian product of <code>LISTX</code> and <code>LISTY</code>, and then depending on the value of <code>LISTY</code> in each tuple, I'd like to add either the product of <code>LISTJ</code>, or the product of <code>LISTK</code>. All as named key/value pairs, or as lists where <code>[0]</code> is the key and <code>[1]</code> is the value.</p>
  
  <p>i.e. If <code>X == LISTA</code>, <code>Y == LISTB</code>, <code>J == LISTC</code>, <code>K == LISTE</code>, and we assume if there is value <code>B1</code> we get the product with <code>J(LISTC)</code> and <code>B2</code> we get the product with <code>K(LISTE)</code>, then we should get something like...</p>
  
  <pre><code>[{'LISTA': A1, 'LISTB': B1 'LISTC': C1},
   {'LISTA': A1, 'LISTB': B2 'LISTE': E1},
   {'LISTA': A1, 'LISTB': B2 'LISTE': E2},
   {'LISTA': A1, 'LISTB': B2 'LISTE': E3},
   {'LISTA': A2, 'LISTB': B1 'LISTC': C1},
   {'LISTA': A2, 'LISTB': B2 'LISTE': E1},
   {'LISTA': A2, 'LISTB': B2 'LISTE': E2},
   {'LISTA': A2, 'LISTB': B2 'LISTE': E3}]
  </code></pre>
  
  <p>I think it's probably something along these lines, where we first get the product, and then use a generator in order to return a dict for each tuple in that product, along with the assigned <code>k,v</code> pair from <code>j</code> or <code>k</code>. However, I can't figure out how to make this more generic than having 'y_val' in the arguments, and I'm also not sure how to assign the keys in <code>J_OR_K</code>, rather than just ending up with a tuple like <code>(('LISTA', A1), ('LISTB', B1), C1)</code>.</p>
  
  <pre><code>import itertools as it
  
  def J_OR_K(P, y_val1, y_val2, j_list, k_list):
      for k, v in P:
          for x in {y_val1:j_list, y_val2:k_list}[v]:
              yield {a,b,x}
  
  def my_func(x, y, j, k):
      res = []
      # get cartesian product of x, y
      prod_xy = it.product(it.product(['LISTA'], x), 
                           it.product(['LISTB'], y))
      # get product with EITHER J OR K
      for t in J_OR_K(j, k):
          res.append(t)
  </code></pre>
  
  <p>Although this is similar to a question I've previously <a href="https://stackoverflow.com/questions/47043660/conditional-cartesian-product-of-lists-in-itertools">asked</a>, this is moderately different and more involved - so felt it required a new question.</p>