if key not in d: d[key] = 0 d[key] += count Dictionary of words mapped to how many times the word appears in the file Dictionary is called d1 For example d1["and"] = 8, d1["or"] = 8, d1["cat"]= 3 Invert dictionary to d2 d2[8] = ["and", "or"] d2[3] = ["cat"] to create d2 for (word,cnt) in d1.items(): if cnt not in d2: d2[cnt] = [word] # create list , first time else: # cnt is already in d2 d2[cnt].append(word) if key not in d3: d3[key] = set(value) else: d3[key].add(value) a = set([]) print a set([]) b = set() print b set([]) print a set([]) x = [ 4, 6, 1, 2, 7, 4, 6] y = [a.add(w) for w in x] print y [None, None, None, None, None, None, None] print a set([1, 2, 4, 6, 7]) z = set([w for w in x]) print z set([1, 2, 4, 6, 7])