Simplify code using defaultdict() and itertools.combinations()