Python getting unique pairs from multiple lists of different lengths
<p>Suppose that in Python I have 3 lists: <code>a, b, c</code> of variable lengths. For example : </p>

<pre><code>a=[1,2,3]
b=[4,5,6]
c=[7,8]
</code></pre>

<p>I would like to get every unique combination of TWO elements of the 3 lists above, i. e. </p>

<p><code>[1,4],[1,5],[1,6],[1,7],[1,8],[2,4],[2,5]...</code> and NOT unique combinations of the 3 lists (such as <code>[1,4,7],[1,4,8],...</code>).</p>

<p>I have looked at the <a href="https://stackoverflow.com/questions/8242832/python-all-possible-pairs-of-2-list-elements-and-getting-the-index-of-that-pair">solution here</a> using <code>itertools</code> that is perfectly fine for 2 lists ; however, this solution does not work anymore when including a <code>n</code>th list because the unique combinations are of length <code>n</code>.</p>

<p>Here is what I have tried:</p>

<pre><code>import itertools

a=[1,2,3]
b=[4,5,6]
c=[7,8]

d=list(itertools.product(a,b,c))

[(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (1, 6, 7), (1, 6, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (2, 6, 7), (2, 6, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8), (3, 6, 7), (3, 6, 8)]
</code></pre>

<hr>

<p>Note: Above is just an example and the solution should work for <code>n</code> lists of variable length and with possibly the same value being in different lists... Any idea of how I could do would be greatly appreciated! :)</p>

<hr>

<p>EDIT: as asked by @SirParselot, the elements have to come from different lists</p>