On this page
collections.Counter
Prev: collections.ChainMap | Up: collections | Next: collections.defaultdict
Rel: collections.ChainMap, collections.defaultdict, collections.deque, collections.namedtuple
Ref: docs.python.org — collections.Counter
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import Counter
Signature & Overview
collections.Counter(iterable=None, /, **kwds)
Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.
Methods & Attributes
Counter.clear(self, /)— Remove all items from the dict.Counter.copy(self)— Return a shallow copy.Counter.elements(self)— Iterator over elements repeating each as many times as its count.Counter.fromkeys(iterable, v=None)—Counter.fromkeysmethod/property.Counter.get(self, key, default=None, /)— Return the value for key if key is in the dictionary, else default.Counter.items(self, /)— Return a set-like object providing a view on the dict's items.Counter.keys(self, /)— Return a set-like object providing a view on the dict's keys.Counter.most_common(self, n=None)— List the n most common elements and their counts from the most common to the least. If n is None, then list all element counts.Counter.pop(...)— If the key is not found, return the default if given; otherwise, raise a KeyError.Counter.popitem(self, /)— Remove and return a (key, value) pair as a 2-tuple.Counter.setdefault(self, key, default=None, /)— Insert key with a value of default if key is not in the dictionary.Counter.subtract(self, iterable=None, /, **kwds)— Like dict.update() but subtracts counts instead of replacing them.Counter.total(self)— Sum of the countsCounter.update(self, iterable=None, /, **kwds)— Like dict.update() but add counts instead of replacing them.Counter.values(self, /)— Return an object providing a view on the dict's values.
Usage
from collections import Counter
counts = Counter("abracadabra")
assert counts["a"] == 5
assert counts["z"] == 0 # missing keys return 0 without inserting them
assert counts.most_common(2) == [("a", 5), ("b", 2)]
Gotchas
- Zero and negative counts: Arithmetic (
c1 - c2,+c) drops zero/negative counts, whereasc.subtract(other)preserves them. - Missing keys don't auto-insert: Unlike
defaultdict(int), readingcounts["missing"]returns0without adding"missing"to the dict.
REPL Inspect
from collections import Counter
help(Counter)
Linked from
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/collections/ChainMap
- tuts/python3/stdlib_module/collections/defaultdict
- tuts/python3/stdlib_module/collections/deque
- tuts/python3/stdlib_module/collections/namedtuple
- tuts/python3/stdlib_module/collections/OrderedDict
- tuts/python3/stdlib_module/collections/UserDict
- tuts/python3/stdlib_module/collections/UserList
- tuts/python3/stdlib_module/collections/UserString
Related notes
- tuts/python3/stdlib_module/collections/ChainMap Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/OrderedDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserList links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString links to this page; shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib