I have 2D list in python
list = [[9, 2, 7], [9, 7], [2, 7], [1, 0], [0, 5, 4]]
I would like to get union of list items if there occurs any intersection. For example [9, 2, 7]
, [9, 7]
, [2, 7]
has intersection of more than one digit. The union of this would be [9,2,7]
.
How can i get the final list as follows in efficient way ?
finalList = [[9,2,7], [0, 1, 5, 4]]
N.B. order of numbers is not important.