Get all pairs from elements in sublists
<p>I have a list of sublists. I need all possible pairs between the elements in the sublists. For example, for a list like this: </p>

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

<p>The result should be: </p>

<pre><code>result=[[1,4], [1,5], [1,6], 
        [2,4], [2,5], [2,6], 
        [3,4], [3,5], [3,6], 
        [4,6], [5,6]]
</code></pre>

<p>The lists (and sublists) are of various, random lengths. </p>

<p>You can do the following</p>

<pre><code>&gt;&gt;&gt; from itertools import chain
&gt;&gt;&gt; a=[[1,2,3],[4,5],[6]]
&gt;&gt;&gt; b=[]
&gt;&gt;&gt; for index, item in enumerate(a):
...    b.extend([[i, j] for i in item for j in chain.from_iterable(a[index+1:])])
&gt;&gt;&gt; b
[[1, 4],
 [1, 5],
 [1, 6],
 [2, 4],
 [2, 5],
 [2, 6],
 [3, 4],
 [3, 5],
 [3, 6],
 [4, 6],
 [5, 6]]
</code></pre>

python