How can I optimize the intersections between lists with two elements and generate a list of lists without duplicates in python?
<p>I need help with my loop. In my script I have two huge lists (~87.000 integers each) taken from an input file. </p>

<p>Check this example with a few numbers:</p>

<p>We have two lists:</p>

<p><code>nga = [1, 3, 5, 34, 12]</code></p>

<p><code>ngb = [3, 4, 6,  6,  5]</code></p>

<p>The order of these two lists matters because each position is related with the same position in the other list, so <code>1</code> in <code>nga</code> is related with <code>3</code> in <code>ngb</code>, <code>3</code> with <code>4</code>, etc...</p>

<p>So what I want is this output:</p>

<p><code>listoflists = [[1, 3, 4], [5, 6, 12, 34]]</code></p>

<p>What I have so far is this loop:</p>

<pre><code>listoflists = []

for p in range(0, len(nga)):
    z = [nga[p], ngb[p]]
    for a, b in zip(nga, ngb):
        if a in z:
            z.append(b)
        else:
            pass
        if b in z:
            z.append(a)
        else:
            pass
    listoflists.append(z)
</code></pre>

<p>The problem appears when I used the whole lists, because it crashed and give me a Segmentation fault error. So, what can I do? </p>

<p>Thanks in advance.</p>

<p>I solved my problem with this beautiful function:</p>

<pre><code>net = []
for a, b in zip(nga, ngb):
    net.append([a, b])

def nets_super_gen(net):
    not_con = list(net)
    netn = list(not_con[0])
    not_con.remove(not_con[0])
    new_net = []
    while len(netn) != len(new_net):
        new_net = list(netn)
        for z in net:
            if z[0] in netn and z[1] not in netn:
                netn.append(z[1])
                not_con.remove(z)
            elif z[0] not in netn and z[1] in netn:
                netn.append(z[0])
                not_con.remove(z)
            try:
                if z[0] in netn and z[1] in netn:
                    not_con.remove(z)
            except ValueError:
                pass
    return(netn, not_con)

list_of_lists, not_con = nets_super_gen(net)
</code></pre>

python list optimization intersection