Python Forum

Full Version: List of lists - merge sublists with common elements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Assume I have list1 as follows:

list1 = [['a','b'],['c','d'],['b','e'],['f','g'],['a','h'],['i','c']]
I want to merge the sublists that have common elements, so based on the above example the resulting list will be

list2 = [['a','b','e','h'],['c','d','i'],['f','g']]
I could do things like parse each element separately and do comparisons all over etc. but the input list can be pretty long (maybe many hundreds or even millions of elements overall) Is there an efficient way to do this in python?
This is a version of the problem of finding the connected components of a graph. One approach would be to use a library that already has a function to do that, such as networkx.
>>> import networkx as nx
>>> g = nx.Graph()
>>> ipath = [['a','b'],['c','d'],['b','e'],['f','g'],['a','h'],['i','c']]
>>> for p in ipath:
...     g.add_edges_from(zip(p, p[1:]))
... 
>>> for c in nx.connected_components(g):
...     print(c)
... 
{'e', 'h', 'a', 'b'}
{'i', 'c', 'd'}
{'g', 'f'}