Invert a dictionary when some of the original values ​​are the same

advertisements

Say I have a dictionary called word_counter_dictionary that counts how many words are in the document in the form {'word' : number}. For example, the word "secondly" appears one time, so the key/value pair would be {'secondly' : 1}. I want to make an inverted list so that the numbers will become keys and the words will become the values for those keys so I can then graph the top 25 most used words. I saw somewhere where the setdefault() function might come in handy, but regardless I cannot use it because so far in the class I am in we have only covered get().

inverted_dictionary = {}
for key in word_counter_dictionary:
    new_key = word_counter_dictionary[key]
    inverted_dictionary[new_key] = word_counter_dictionary.get(new_key, '') + str(key)
    inverted_dictionary

So far, using this method above, it works fine until it reaches another word with the same value. For example, the word "saves" also appears once in the document, so Python will add the new key/value pair just fine. BUT it erases the {1 : 'secondly'} with the new pair so that only {1 : 'saves'} is in the dictionary.

So, bottom line, my goal is to get ALL of the words and their respective number of repetitions in this new dictionary called inverted_dictionary.


A defaultdict is perfect for this

word_counter_dictionary = {'first':1, 'second':2, 'third':3, 'fourth':2}
from collections import defaultdict

d = defaultdict(list)
for key, value in word_counter_dictionary.iteritems():
    d[value].append(key)

print(d)

Output:

defaultdict(<type 'list'>, {1: ['first'], 2: ['second', 'fourth'], 3: ['third']})