If I have a following dictionary: {"1": {"2", "3"}, "2": {"3", "4"}, "3": {"2", "4"}} In this dictionary, the key references the values in the set. How do I construct a new dictionary which defines the references to the values above: {"1": {}, "2": {"1", "3"}, "3": {"1", "2"}, "4": {"2", "3"}} This is what I currently do: for k,v in data.items(): if len(v) > 0: for p in v: references[p].add(k) else: for p,v in references.items(): v.add(k) Any better / optimized /pythonic way of doing it? Continue reading...