Learn Python
>>> modules_packages.collections_module

The collections Module in Python

#concept

The collections module extends Python's built-in data structures with specialized alternatives suited to particular common needs — a dictionary with a default value for missing keys, a fast double-ended queue, and an automatic item counter, among others already touched on (like namedtuple) elsewhere in this course.

#how it works
1

Counter(iterable)

Counts how many times each item appears, returning a dictionary-like object of item-to-count pairs.

2

defaultdict(default_factory)

Works like a regular dictionary, but automatically creates a default value for any key accessed for the first time, instead of raising a KeyError.

3

deque — a double-ended queue

Supports fast additions and removals from both ends, unlike a plain list, which is slow to modify at the front.

4

OrderedDict

Preserves insertion order explicitly — regular dictionaries do this by default since Python 3.7 too, so it’s mostly needed now for its few extra order-related methods.

5

namedtuple also lives here

Already covered in its own topic, it’s part of this same module.

#example
from collections import Counter, defaultdict, deque
 
votes = ["red", "blue", "red", "green", "blue", "red"]
print(Counter(votes))
 
groups = defaultdict(list)
groups["fruits"].append("apple")
groups["fruits"].append("banana")
print(groups)
 
queue = deque([1, 2, 3])
queue.appendleft(0)
queue.append(4)
print(queue)
Output
Counter({'red': 3, 'blue': 2, 'green': 1})
defaultdict(<class 'list'>, {'fruits': ['apple', 'banana']})
deque([0, 1, 2, 3, 4])
1Counter(votes) tallies every value in the list, showing "red" appeared 3 times, "blue" 2 times, and "green" once, sorted by count.
2groups["fruits"].append("apple") works immediately, even though "fruits" was never explicitly created first — defaultdict automatically supplies an empty list the first time a new key is accessed.
3queue.appendleft(0) adds to the front and .append(4) adds to the back, both efficiently, showing deque handling both ends of the collection with ease.
#use cases
Counting word frequencies, votes, or any tally of repeated items, using Counter instead of manual dictionary bookkeeping
Grouping items by a key without first checking whether that key already exists, using defaultdict
Implementing a queue or a sliding window efficiently, using deque instead of a plain list
Reaching for one of these specialized structures instead of reinventing similar behavior manually with a plain dict or list

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.