How do you convert a list of nested lists into a list of lists with unique data?
<p>After looping my brains out creating reference dictionaries and multi-nested lookup lists I've decided that there has to be an easier way to do this. I can't be the first person to do this type of conversion. I don't even know where to start looking in the docs for a solution.</p>

<p>I have a system that is outputting the following data.</p>

<pre><code>initial_data = [
    [21,[[1],[2,3],[6],[7]]],
    [22,[[4,5],[6,7]],
    [23,[[1],[4,5],[6],[7]]],
    [24,[[1],[2,3,4],[6],[7]]],
]
</code></pre>

<p>I have another system that expects the data in the following format (order does not matter).</p>

<pre><code>return_data = [
    [21,[1,2,6,7]],
    [21,[1,3,6,7]],
    [22,[4,6]],
    [22,[4,7]],
    [22,[5,6]],
    [22,[5,7]],
    [23,[1,4,6,7]],
    [23,[1,5,6,7]],
    [24,[1,2,6,7]],
    [24,[1,3,6,7]],
    [24,[1,4,6,7]],
]
</code></pre>

<p>You can use the <code>itertools.product</code>, which produces </p>

<blockquote>
  <p>Cartesian product of input iterables.</p>
  
  <p>Roughly equivalent to nested for-loops in a generator expression. For
  example, product(A, B) returns the same as ((x,y) for x in A for y in
  B).</p>
</blockquote>

<p>Use it on the second element of each sublist should produce what you need:</p>

<pre><code>from itertools import product
[[k, p] for k, v in initial_data for p in product(*v)]

# [[21, (1, 2, 6, 7)],
#  [21, (1, 3, 6, 7)],
#  [22, (4, 6)],
#  [22, (4, 7)],
#  [22, (5, 6)],
#  [22, (5, 7)],
#  [23, (1, 4, 6, 7)],
#  [23, (1, 5, 6, 7)],
#  [24, (1, 2, 6, 7)],
#  [24, (1, 3, 6, 7)],
#  [24, (1, 4, 6, 7)]]
</code></pre>

python unique nested-lists