title: Handle Transitivity in Python
body: <p>I have set of pairwise relationship something like this</p>
  
  <pre><code>col_combi = [('a','b'), ('b','c'), ('d','e'), ('l','j'), ('c','g'), 
               ('e','m'), ('m','z'), ('z','p'), ('t','k'), ('k', 'n'), 
               ('j','k')]
  </code></pre>
  
  <p>Number of such relationship is big enough to check it individually. These tuple indicates that both values are same. I would like to apply transitivity and find out common groups. Output would be like following:</p>
  
  <pre><code>[('a','b','c','g'), ('d','e','m','z','p'), ('t','k','n','l','j')]
  </code></pre>
  
  <p>I tried following code but it has bug, </p>
  
  <pre><code>common_cols = []
  common_group_count = 0
  
  for (c1, c2) in col_combi:
      found = False
      for i in range(len(common_cols)):
          if (c1 in common_cols[i]):
              common_cols[i].append(c2)
              found = True
              break
          elif (c2 in common_cols[i]):
              common_cols[i].append(c1)
              found = True
              break
      if not found:
          common_cols.append([c1,c2])
  </code></pre>
  
  <p>Output of above code is following</p>
  
  <pre><code>[['a', 'b', 'c', 'g'], ['d', 'e', 'm', 'z', 'p'], ['l', 'j', 'k'], ['t', 'k', 'n']]
  </code></pre>
  
  <p><strong>I know why this code is not working.</strong> So I would like to know how can I perform this task.</p>
  
  <p>Thanks in advance</p>