Counter
A dictionary from the collections module that counts how many times each item occurs.
In more detail
Counter(words) tallies a list in one call, and most_common(3) returns the three most frequent items with their counts. Missing items count as 0 rather than raising KeyError.
Example
from collections import Counter
Counter("mississippi").most_common(2)Related terms
- collections module — The standard library module of extra container types, including Counter, defaultdict, namedtuple and deque.
- dictionary — A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
Taught in
- While Loops, And The General Range — Hands-on Python: Complete Video Course & Book
- A Standard Library Tour — Hands-on Python: Complete Video Course & Book
- Shipping: scripts, entry points and git — Hands-on Python: Complete Video Course & Book