How to breakup a list of list in a given way in Python
<p>Given a list of list of integers, task is to write a function which will output another list of list of integers. It's hard to tell the nature of the desired output in words, I am writing some examples:</p>

<pre><code># list with one element will be returned
&gt;&gt;&gt; func([[1, 2, 3, 4]])
[[1, 2, 3, 4]]

&gt;&gt;&gt; func([[1], [1, 2]])
[[1], [2]]

&gt;&gt;&gt; func([[1, 2], [1]])
[[1], [2]]

&gt;&gt;&gt; func([[1], [1, 2, 3]])
[[1], [2, 3]]

&gt;&gt;&gt; func([[1], [1, 2, 3], [1, 4]])
[[1], [2, 3], [4]]

&gt;&gt;&gt; func([[1, 2], [2, 3]])
[[1], [2], [3]]

&gt;&gt;&gt; func([[1], [2], [1, 2, 3]]) 
[[1], [2], [3]]

&gt;&gt;&gt; func([[1, 2], [1, 2, 3]])
[[1, 2], [3]]

&gt;&gt;&gt; func([[1], [1, 2], [1, 2, 3]])
[[1], [2], [3]]
</code></pre>

<p>(UPDATE) You can use the following preconditions:</p>

<ol>
<li><p>Each inner list contains integers already sorted and there is no duplicate entry.</p></li>
<li><p>The outer list has no duplicate entry.</p></li>
</ol>

<p>(UPDATE) As you asked, here's what I think I'm stuck with:</p>

<p>This is a problem on some optimization on a Directed Graph, with numbers as nodes and inner lists as starting points of edges (the outer list is the set of starting points of all edges). Now, you might ask, "How there be multiple start points to a single edge, which is shown in some test cases?" </p>

<p>This is what I am trying to do: for <code>func ([1, 2])</code> the node 1 &amp; node 2 can be merged to a single node. The output <code>[1, 2]</code> shows that these two can be merged.</p>

<p>Now look at <code>func ([[1], [1, 2]])</code>. The 2nd inner list tries to merge node 1 &amp; 2 together, but the 1st inner list says node 1 cannot be merged to anything. So, the output is <code>[[1], [2]]</code>, indicating that node 1 &amp; node 2 are to be kept separated.</p>

<p>For <code>func ([[1], [2, 3], [1, 2, 3]])</code>, node 1 is to be separated, but node 2 &amp; 3 can be merged; so the output would be <code>[[1], [2, 3]]</code></p>

<p>In case of <code>func ([[1, 2], [2, 3]])</code>, neither node 1 &amp; 2 nor 2 &amp; 3 can be merged as node 1 &amp; 3 can be merged, so the expected result is <code>[[1], [2], [3]]</code>.</p>

<p>There is a list of integers also, which comprises of end points of vertices, each integer corresponds to each inner list. When elements of an inner list are merged to one, there remains only 1 edge. When they are separated, there are singleton lists, and elements from each of which is taken as starting points; the list of end points are updated accordingly.</p>

<p>I think it will help you to realize my needs.</p>
