defaultdict({' '( ['hello'])' '( ['world'])}when we print dict at the endwe see that the counter really was working this examplewhile succinctly demonstrating how to create our own function for defaultdictis not actually very good codeusing global variable means that if we created four different defaultdict segments that each used tuple_counterit would count the number of entries in all dictionariesrather than having different count for each one it would be better to create class and pass method on that class to defaultdict counter you' think that you couldn' get much simpler than defaultdict(int)but the " want to count specific instances in an iterableuse case is common enough that the python developers created specific class for it the previous code that counts characters in string can easily be calculated in single linefrom collections import counter def letter_frequency(sentence)return counter(sentencethe counter object behaves like beefed up dictionary where the keys are the items being counted and the values are the number of such items one of the most useful functions is the most_common(method it returns list of (keycounttuples ordered by the count you can optionally pass an integer argument into most_common(to request only the top most common elements for exampleyou could write simple polling application as followsfrom collections import counter responses "vanilla""chocolate""vanilla""vanilla""caramel""strawberry""vanillaprint