On this page

Collections Module

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Purpose of this lesson​

The collections module exists because Python’s built‑in list and dict types are general‑purpose tools. They work, but they are not always the best tool for real‑world problems.

The structures in collections solve specific, recurring patterns:

This lesson teaches you how to recognize those patterns and choose the right structure instead of reinventing logic with plain lists and dicts.


Counter — when you need to measure what’s happening​

Why Counter exists​

In real programs, you constantly need to answer questions like:

You could write a loop, check if a key exists, initialize it, increment it…
But that’s boilerplate. Counter exists because frequency counting is so common that it deserves a dedicated tool.

How Counter changes the way you think​

Instead of thinking:

“I need a dict and a loop to count things.”

You think:

“I need a frequency distribution → that’s a Counter.”

Example:


from collections import Counter



words = ["a", "b", "a", "c", "b", "a"]

counts = Counter(words)

Now counts is not just a dict—it’s a frequency model.
You can immediately ask meaningful questions:


counts.most_common(2)

This gives you the top items without writing sorting logic.

Counter encourages you to think in terms of data analysis, not dict manipulation.


defaultdict — when missing keys shouldn’t be errors​

The problem defaultdict solves​

A normal dict forces you to check whether a key exists before using it.
This leads to patterns like:


if key not in d:

    d[key] = []

d[key].append(value)

You write this so often that it becomes noise.

defaultdict removes that noise by creating the default value automatically.

How defaultdict changes your mental model​

Instead of thinking:

“I need to check if the key exists.”

You think:

“This is a grouping problem → I need a defaultdict(list).”

Example: grouping items by category:


from collections import defaultdict



groups = defaultdict(list)



for name, score in [("Alice", 10), ("Bob", 8), ("Alice", 7)]:

    groups[name].append(score)

You focus on the logic, not the bookkeeping.

The same applies to counting:


counts = defaultdict(int)

counts["a"] += 1

defaultdict is your go‑to tool whenever you want to accumulate values into a structure.


deque — when you need a real queue or stream​

Why deque exists​

Lists are optimized for fast access and fast appends at the end.
They are terrible at inserting or removing from the beginning.

A deque (double‑ended queue) is designed for:

How deque changes your approach​

Instead of thinking:

“I’ll use a list and hope it’s fast enough.”

You think:

“This is a queue/stream → I need a deque.”

Example:


from collections import deque



dq = deque([1, 2, 3])

dq.append(4)

dq.appendleft(0)

dq.pop()

dq.popleft()

A deque behaves like a pipeline buffer—perfect for producer/consumer systems, simulations, and time‑series processing.

It also supports fixed‑length queues:


dq = deque(maxlen=3)

This automatically discards old items, which is ideal for sliding windows.


namedtuple — when you need structure without the weight of a class​

Why namedtuple exists​

Sometimes you need a small, simple data record:

You could use a tuple, but then you get unreadable code like p[0].
You could use a class, but that’s overkill for tiny data containers.

namedtuple gives you:

How namedtuple changes your thinking​

Instead of:

“I’ll use a tuple and try to remember the index.”

You think:

“This is a small record → I’ll use a namedtuple.”

Example:


from collections import namedtuple



Point = namedtuple("Point", ["x", "y"])

p = Point(10, 20)

Now your code reads like a data model, not a tuple puzzle.


OrderedDict — when order is part of the logic​

Why OrderedDict used to matter​

Before Python 3.7, dicts did not preserve insertion order.
OrderedDict was the only way to maintain order.

Today, regular dicts preserve order, so OrderedDict is no longer needed for everyday use.

When OrderedDict still matters​

OrderedDict still provides specialized ordering operations that normal dicts do not:

Example:


from collections import OrderedDict



od = OrderedDict()

od["a"] = 1

od["b"] = 2

od.move_to_end("a")

You use it when order is not just a property but a behavior.


Putting it all together — solving a real problem​

Imagine you want to analyze a list of words:

With plain dicts and lists, this becomes messy.
With collections, the solution expresses the intent clearly:


from collections import Counter, defaultdict



words = ["apple", "banana", "apricot", "blueberry", "cherry"]



# Count words

counts = Counter(words)



# Group by first letter

groups = defaultdict(list)

for w in words:

    groups[w[0]].append(w)

You now have:

The code reads like a description of the problem, not a workaround.


Best practices​

These structures let you write code that is cleaner, faster, and more expressive than reinventing logic with plain dicts and lists.


Summary​

The collections module is not about memorizing five data structures.
It’s about recognizing patterns:

Once you learn to see these patterns, your code becomes more Pythonic, more efficient, and easier to maintain.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Itertools & Functools

Next

Subprocess & System Interaction