AI engineering glossary · Python advanced
What is counter?
Counter: A dictionary subclass in the collections module that counts how many times each item appears in an iterable. It offers helpers such as most_common() for ranking frequent items. It works like a tally sheet keyed by item, ideal for word frequencies.
Counter: an example
Counter(words).most_common(3) returns the three most frequent words in a list of tokens.
Related terms
- collections module
- defaultdict
- Dictionary
- Word frequency
More Python advanced terms
- Dataclass
- Decorator
- Decorator with arguments
- defaultdict
- Descriptor
- Duck typing
- Dunder method
- Encapsulation
Where you learn it
Counter is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.