wordsFreq = {}
words = []
while True:
inputWord = raw_input()
if (inputWord != ""):
words.append(inputWord)
else:
break
for word in words:
wordsFreq[word] = wordsFreq.get(word, 0) + 1
for word,freq in wordsFreq.items():
print word + " - " + str(freq)
Apparently my words[] and the for loop is redundant but I had no further explanation than that, can anyone explain to me why it is redundant?
You can skip the step of building a list of words and instead directly create the frequency dict as the user is entering words. I’ve used
defaultdictto avoid having to check if a word has already been added.If you aren’t allowed to use
defaultdict, it could look like this: