JD, you probably need the algorithm for connected components of an undirected graph.
For example you can do that with my graph lib: http://sourceforge.net/projects/pynetwork/ from graph import Graph g = Graph() data = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['e', 'k'], ['c', 'u'], ['b', 'p']] g.addArcs(data, bi=True) print g.connectedComponents() # Output: [['a', 'b', 'g', 'p'], ['c', 'u', 'd'], ['e', 'k', 'f']] Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list