How to merge lists of tuples?
<p>I use python 3</p>

<p>I have a list L, which contains another lists, which contains turples of point (x,y):</p>

<pre><code>L=[A, B, C, D ...]
A = [(1,1),(3,3),(4,5)...]
B=[(1,2),..(6,7)]
</code></pre>

<p>I want to merge all lists from L, which have a adjacent points.
As above: we must to union A,B because they have a adjacent points: (1,1) from A, and (1,2) from B. And obtain: L =[A+B, C, ...]</p>

<p>I tried code something like this:</p>

<pre><code>if (x-1,y-1) in L[i] or (x-1,y) in L[i] or (x-1,y+1) in L[i] or (x,y-1) in L[i] or (x,y+1) in L[i] or (x+1,y-1) in L[i] or (x+1,y) in L[i] or (x+1,y+1) in L[i]:
</code></pre>

<p>But I can't union all of the list from L.
Help</p>

<p>*
adjacent points are point , distance between them = 1, for example (1,1) and (1,2). (5,6) and (6,7). (8,7) and (7,7)</p>

<p>This is my crack at your algorithm:</p>

<pre><code>def adjacent(list_1, list_2):
    for elem_1 in list_1:
        for elem_2 in list_2:
            if abs(elem_1[0] - elem_2[0]) == 1 or\
               abs(elem_1[1] - elem_2[1]) == 1:
                   return True
    return False

def join_adjacent_points(L):
    result = []
    while len(L) &gt; 0:
        result.append(L[0])
        L = L[1:]
        i = 0
        while i &lt; len(L):
            if adjacent(L[i], result[-1]):
                result[-1] += L[i]
                del L[i]
                i = 0
            i += 1
    return result
</code></pre>

python python-3.x