from collections import OrderedDict from operator import itemgetter def words(): filename = '2013_Wages_for_Facebook_[EN].txt' path = './manifestos/'+filename file = open(path, 'r') manifesto = file.read() manifesto = manifesto.replace('\n', ' ') manifesto = manifesto.lower() manifesto = manifesto.strip() words = manifesto.split(' ') new_dict = dict() for word in words: if word in new_dict: # Present so add 1 to the count of word. new_dict[word] = new_dict[word] + 1 else: # Add the word to dictionary with count 1 new_dict[word] = 1 result = OrderedDict(sorted(new_dict.items(), key=itemgetter(1), reverse=True)) return result if __name__ == '__main__': result = words() print('result:', result)