data = """ID1 ID2 ID3
ID1 ID4 ID5
ID3 ID5 ID7 ID6"""
groups = collections.defaultdict(set)
for row in data.split('\n'):
    cols = row.split()
for groupcol in cols:
    for col in cols:
        if col is not groupcol:
            groups[groupcol].add(col)
pprint.pprint(dict(groups))
Better to add item to a set, or convert final list to a set?