Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

Counter

Counter is a dict subclass for tallying hashable objects. It maps each element to its count and supports specialized counting, comparison, and multiset operations.

How it works

Counter stores each hashable element as a dictionary key and its tally as the associated value. It accepts an iterable or mapping, supports dictionary-style access and specialized tally methods, and provides arithmetic and multiset operations for combining counters.

Syntax

class collections.Counter([iterable-or-mapping])

Parameters

NameTypeDescription
iterable_or_mappingiterable or mappingOptional iterable whose elements are counted, or mapping/Counter providing element-count pairs.

Returns

A Counter mapping hashable elements to their counts.

Key points

  • Counter is dictionary-compatible and requires its counted elements to be hashable.
  • Each distinct element is represented once as a key, with its tally stored as the value.
  • Use Counter when tallying repeated values is the main task.
  • The + and - operations combine matching keys arithmetically.
  • Intersection keeps the lower matching count, while union keeps the higher one.
  • Counters can be compared based on their corresponding element counts.

Common mistakes

  • Counts may be zero or negative, so a Counter is not restricted to positive frequencies.
  • Passing an iterable counts its elements, while a mapping supplies explicit element-to-count pairs.
  • Counter has specialized operations in addition to the ordinary dictionary interface.

Example: Counting and updating observations

from collections import Counter

counts = Counter("banana")
print(counts.most_common(2))
counts.update("band")
print(counts["a"], counts.total())

Counts characters, selects the two most frequent entries, adds more observations, and totals all counts.

When to use it

Frequency counting

Tally repeated values in a sequence or stream.

Merging counts

Combine independent tally results using arithmetic operations.

Ranking frequencies

Find the most frequent elements in collected data.

Interview questions

QWhat is Counter, and how can it be initialized?

Counter is a dict subclass that maps hashable elements to their counts. An iterable contributes one count for each element, while a mapping or another Counter initializes explicit counts.

Follow-up: What happens when a missing key is read? The page documents Counter's dictionary-like behavior and examples; verify the missing-key rule in the current method details before relying on it.

QHow do Counter arithmetic operations work?

The class provides arithmetic operations for combining counters. Addition and subtraction combine corresponding counts, while intersection and union use the minimum and maximum counts respectively.

Follow-up: Which results represent multisets? The documentation describes multisets as counters whose counts are greater than zero.

QWhy use Counter instead of a normal dictionary?

Counter includes methods beyond dictionaries for tally workflows, including operations for examining repeated elements and combining counts. It also supports dictionary operations because it subclasses dict.

Follow-up: What should be checked when counts can be nonpositive? Determine whether the particular method includes zero or negative counts, since multiset results are described using positive counts.

  • collections.defaultdict
  • collections.deque
  • dict
  • Counter.elements()
  • Counter.most_common()
  • Counter.update()

Source: collections — Container datatypes — Python 3.14.6 documentation. Checked 2026-10-16.