Skip to content

Counter

A counter returns a defaultdict(int) of the items passed to it as a parameter

from collections import Counter

counter_list = Counter([0, 1, 2, 3, 0])
counter_list
Counter({0: 2, 1: 1, 2: 1, 3: 1})
document = "Hello this is a test and this is another test."

word_count = Counter(document.split(' '))
word_count
Counter({'Hello': 1,
         'this': 2,
         'is': 2,
         'a': 1,
         'test': 1,
         'and': 1,
         'another': 1,
         'test.': 1})