def union_find(lis):
    lis = map(set, lis)
unions = []
for item in lis:
    temp = []
for s in unions:
    if not s.isdisjoint(item):
        item = s.union(item)
else:
    temp.append(s)
temp.append(item)
unions = temp
return unions
if __name__ == '__main__':
    l = [[1, 2], [2, 3], [4, 5], [6, 7], [1, 7]]
print(union_find(l))
Union find implementation using Python