title: Python Connected Components edges list
body: <p>I use these algorithms in python for finding connected components from edges. </p>
  
  <pre><code>components = []
  
  def connected_components(pairs):
      for a, b in pairs:
          for component in components:
              if a in component:
                  for i, other_component in enumerate(components):
                      if b in other_component and other_component != component: # a, and b are already in different components: merge
                          component.extend(other_component)
                          components[i:i+1] = []
                          break # we don't have to look for other components for b
                  else: # b wasn't found in any other component
                      if b not in component:
                          component.append(b)
                  break # we don't have to look for other components for a
              if b in component: # a wasn't in in the component 
                  component.append(a)
                  break # we don't have to look further
          else: # neither a nor b were found
              components.append([a, b])
      return components
  </code></pre>
  
  <p>This algorithms return components like this : </p>
  
  <pre><code>[ [n1,n2,n4],[n3,n5] ]
  </code></pre>
  
  <p>I would like to have the list of all edges in connected components like this : </p>
  
  <pre><code>[ [(n1,n2),(n2,n4),(n4,n1)],[(n3,n5)] ] 
  </code></pre>
  
  <p>in the same order of the previous list but i don't know how creates this list</p>
  
  <p>Thank you for your help.</p>