I have a list of tuples, let's say
tuplist = [('a','b'),('a','c'),('e','f'),('f','c'),('d','z'),('z','x')]
I am trying to get the following :
('a','b','c','e','f'),('d','z','x')
This is all the elements linked together (like a tree in graph theory) The order inside the above tuples (can be lists too) does not matter.
I have managed to get a dict of all links for a single element but I am struggling to get to the final result in a clean and efficient way... Here is my code so far :
ll=[('a','b'),('a','c'),('e','f'),('f','c'),('d','z'),('z','x')]
total = {}
total2={}
final=[]
for element in set([item for sublist in ll for item in sublist]):
tp = []
for tupl in ll:
if element in tupl:
tp.append(tupl)
tp = list(frozenset([item for sublist in tp for item in sublist]))
total[element] = tp
print(total)