May-09-2021, 02:29 AM
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?