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

Python · Module

collections

Python’s collections module provides specialized container types as alternatives to built-in dict, list, set, and tuple. It includes tools for queues, counting, layered mappings, named records, default values, and container subclassing.

How it works

The collections module supplies specialized alternatives to built-in containers. Its types cover named tuple records, double-ended queues, layered mappings, counting, insertion-aware dictionaries, factories for missing values, and wrappers that simplify container subclassing.

Syntax

import collections
from collections import namedtuple, deque, ChainMap, Counter, OrderedDict, defaultdict, UserDict, UserList

Key points

  • The module groups purpose-built containers that complement Python’s ordinary built-in collection types.
  • namedtuple() creates tuple subclasses whose positions can also be accessed through descriptive field names.
  • deque is suitable for queue and stack patterns that operate on either end.
  • ChainMap presents multiple mappings as one combined, updateable view without first merging them.
  • Counter stores frequencies for hashable items using dictionary-style keys.
  • defaultdict obtains a default value from a supplied factory when a key is absent.

Common mistakes

  • A deque is optimized for both ends, but indexing near its middle is slower than indexing at either end.

Example: Specialized container types

from collections import Counter, defaultdict, deque

queue = deque(["job-1", "job-2"])
queue.append("job-3")
print(queue.popleft())

counts = Counter(["python", "python", "go"])
print(counts["python"])

settings = defaultdict(list)
settings["tags"].append("backend")
print(settings["tags"])

This runnable example uses a deque for efficient end operations, Counter to tally hashable values, and defaultdict to create a list automatically for a missing key.

When to use it

Double-ended queues

Process work items from either side of a buffer.

Counting occurrences

Aggregate frequencies for hashable values.

Layered mappings

Combine configuration layers while retaining precedence.

Automatic missing-value handling

Group values under keys without manual initialization.

Interview questions

QWhat is the purpose of the collections module?

collections provides specialized container types that extend or complement dict, list, set, and tuple. Examples include deque, Counter, defaultdict, ChainMap, and namedtuple().

Follow-up: When is deque preferable to list? Use it when frequent appends and pops are needed at both ends, because it is designed for efficient end operations.

QWhen would you choose deque over list?

deque is a list-like double-ended queue. It supports fast appends and pops on either end, while indexed access becomes slower toward the middle.

Follow-up: How can deque memory be bounded? Construct it with maxlen; when full, adding new items discards items from the opposite end.

QHow do Counter and defaultdict differ?

Counter is a dict subclass for counting hashable objects. defaultdict is a dict subclass that calls a factory function to provide missing values.

Follow-up: How does defaultdict differ from dict.get()? defaultdict can create and store a value through its factory when a missing key is accessed; get() does not invoke that factory.

  • namedtuple
  • deque
  • ChainMap
  • Counter
  • OrderedDict
  • defaultdict
  • UserDict
  • UserList

Source: collections — Container datatypes. Checked 2026-10-15.