I have some data that looks something like this: ID1 ID2 ID3 ID1 ID4 ID5 ID3 ID5 ID7 ID6 ... ... where each row is a group. My goal is to have a dictionary for each ID, followed by a set of the other IDs that share >= 1 group with it. For example, this data would return {ID1: [ID2, ID3, ID4, ID5], ID2:[ID1, ID3] ... } I can think of 3 options for this, and I'm wondering which is (generally) best: Check whether an ID is already in the list before adding it Create sets instead of lists, and add each ID to the set Add all IDs to the list, then convert all of the lists to sets at the end. Continue reading...