Python Collections Module Explained: Counter, defaultdict, deque and More, Tested
The Python collections module gives you ready-made containers for counting, grouping, queues and lightweight records. Learn when to use Counter, defaultdict, deque, namedtuple, OrderedDict and ChainMap, with output we ran on Python 3.13.
- Python collections is a standard library module: no install, just
from collections import .... Countercounts things,defaultdictgroups things, anddequeis the right tool for queues.- Draining 100,000 items from the front took about 11 seconds with
list.pop(0)and 3 milliseconds withdeque.popleft()in our run. namedtuplegives readable, immutable records;ChainMaplayers settings without copying them.
Most Python code is built on lists and dictionaries, and that works until you write the same counting loop for the fifth time or watch a queue slow to a crawl. The Python collections module exists for exactly those moments. It ships with Python and adds a handful of specialised containers that are faster, shorter and clearer than the hand-written versions.
Every example below was run on Python 3.13.6 on a Windows laptop, and the outputs are copied from that run. Timings will differ on your machine, but the size of the gaps should not. If you need a refresher on the basics first, read our Python dictionary guide and our comparison of list vs tuple vs set.
What is the collections module?
The collections module is part of the Python standard library. It provides container data types that extend the built-in dict, list and tuple. The ones you will use most are:
- Counter: a dictionary that counts hashable items.
- defaultdict: a dictionary that creates a default value for missing keys.
- deque: a double-ended queue with fast appends and pops at both ends.
- namedtuple: a tuple whose fields have names.
- OrderedDict: a dictionary with extra ordering tools.
- ChainMap: a view that searches several dictionaries in order.
Because Counter, defaultdict and OrderedDict are dictionary subclasses, anything that accepts a dict also accepts them. That makes them easy to drop into existing code.
Counter: count anything in one line
Counting words, status codes or test results is a classic job. Instead of a loop with if key in d, pass any iterable to Counter:
from collections import Counter
words = "the cat sat on the mat the cat ran".split()
c = Counter(words)
print(c)
print(c.most_common(2))
print(c["dog"])
print(c.total())
Counter({'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1, 'ran': 1})
[('the', 3), ('cat', 2)]
0
9
Two details are worth noticing. Looking up a missing key returns 0 instead of raising KeyError, and most_common(n) gives you a sorted top-N list without any extra code. The total() method, added in Python 3.10, returns the sum of all counts.
Counters also support arithmetic, which is handy when you compare two inventories or two test runs:
a = Counter(apples=3, pears=1)
b = Counter(apples=1, kiwis=2)
print(a + b) # Counter({'apples': 4, 'kiwis': 2, 'pears': 1})
print(a - b) # Counter({'apples': 2, 'pears': 1})
print(a & b) # Counter({'apples': 1})
print(a | b) # Counter({'apples': 3, 'kiwis': 2, 'pears': 1})
Subtraction drops any count that falls to zero or below, so a - b has no kiwis. Intersection keeps the minimum of each count and union keeps the maximum. A practical use: count failing test names across two nightly runs and subtract to see which failures are new.
defaultdict: group without checking keys
Grouping records by a key normally needs a check for whether the key exists. A defaultdict takes a factory function, such as list or int, and calls it the first time a missing key is used:
from collections import defaultdict
orders = [("asha", "books"), ("ravi", "pens"), ("asha", "pens"), ("li", "books")]
g = defaultdict(list)
for cust, item in orders:
g[cust].append(item)
print(dict(g))
{'asha': ['books', 'pens'], 'ravi': ['pens'], 'li': ['books']}
You can get the same result with dict.setdefault(cust, []).append(item), and in our run the two dictionaries compared equal. The defaultdict version reads better, and it does not build a throwaway empty list on every call the way setdefault does.
Tip: a plain lookup on a defaultdict creates the key. In our test, reading dd["x"] on an empty defaultdict(int) returned 0 and left {'x': 0} behind. Use "x" in dd or dd.get("x") when you only want to check.
deque: a real queue with both ends open
A deque (pronounced “deck”) supports fast append, appendleft, pop and popleft. It also has two features lists lack: a maximum length and rotation.
from collections import deque
last3 = deque(maxlen=3)
for n in range(1, 7):
last3.append(n)
print(last3) # deque([4, 5, 6], maxlen=3)
r = deque([1, 2, 3, 4, 5])
r.rotate(2)
print(r) # deque([4, 5, 1, 2, 3])
With maxlen set, adding to a full deque silently drops an item from the other end. That makes it perfect for a “last N log lines” buffer, a moving average or recent search history. rotate is useful for round-robin scheduling, for example cycling through test workers.
list vs deque: the timing that matters
Lists are fast at the end but slow at the front, because removing the first item shifts every other item left. We drained a container from the front with each approach and timed it with timeit:
| Items | list.pop(0) | deque.popleft() |
|---|---|---|
| 20,000 | 0.408 s | 0.0006 s |
| 50,000 | 2.566 s | 0.0014 s |
| 100,000 | 11.362 s | 0.0030 s |
Doubling the items roughly quadrupled the list time, which is the signature of quadratic work, while the deque time grew in a straight line. At 100,000 items the deque was several thousand times faster. If your code processes jobs, messages or breadth-first search nodes in arrival order, use a deque.
The trade-off: deques are slower than lists for random access in the middle, such as d[50000], and they do not support slicing. Use a list when you index often and a deque when you push and pop at the ends.
namedtuple: readable records
A tuple like (3, 4) works, but p[0] says nothing about what the value means. namedtuple builds a small class with named fields:
from collections import namedtuple
Point = namedtuple("Point", "x y")
p = Point(3, 4)
print(p, p.x, p._asdict(), p._replace(x=10))
p.x = 5
Point(x=3, y=4) 3 {'x': 3, 'y': 4} Point(x=10, y=4)
AttributeError: can't set attribute
Named tuples are immutable, use as little memory as plain tuples, and still unpack like tuples. _replace returns a new copy with one field changed. If you need mutable fields, defaults with validation or methods, a dataclass is usually the better fit; see our Python dataclasses guide. The typed alternative typing.NamedTuple gives the same object with type hints.
OrderedDict: still useful after Python 3.7
Since Python 3.7, normal dictionaries keep insertion order, so many people assume OrderedDict is obsolete. It is not, for two reasons we confirmed:
from collections import OrderedDict
od = OrderedDict(a=1, b=2, c=3)
od.move_to_end("a")
print(list(od)) # ['b', 'c', 'a']
print(dict(a=1, b=2) == dict(b=2, a=1)) # True
print(OrderedDict(a=1, b=2) == OrderedDict(b=2, a=1)) # False
First, move_to_end and popitem(last=False) make it a simple base for a least-recently-used cache. Second, equality between two OrderedDicts is order-sensitive, while plain dicts ignore order. That matters when the order itself is part of the data, such as column order in a report.
ChainMap: layered settings
Configuration often comes from several places: command-line flags override environment variables, which override defaults. ChainMap searches a list of dictionaries in order without merging them:
from collections import ChainMap
defaults = {"theme": "light", "lang": "en", "debug": False}
env = {"debug": True}
cli = {"lang": "fr"}
cfg = ChainMap(cli, env, defaults)
print(cfg["theme"], cfg["lang"], cfg["debug"]) # light fr True
Each lookup checks cli first, then env, then defaults. Because nothing is copied, a change to any of the source dictionaries shows up immediately. Writes go only to the first mapping, which keeps your defaults safe. This pairs well with the patterns in our environment variables guide.
Python collections cheat sheet
| Need | Use | Instead of |
|---|---|---|
| Count items, top N | Counter |
Manual dict loop and sort |
| Group values by key | defaultdict(list) |
if key not in d checks |
| Queue or sliding window | deque, maxlen |
list.pop(0) |
| Small immutable record | namedtuple |
Bare tuple with index access |
| Reorder or order-aware equality | OrderedDict |
Rebuilding a dict |
| Layered config lookups | ChainMap |
Copying and merging dicts |
The module also contains UserDict, UserList and UserString, which are wrapper classes meant for subclassing when you want to customise behaviour safely. Most everyday code never needs them.
Common mistakes with collections
- Creating keys by accident. Reading a missing key from a
defaultdictinserts it. Later code that loops over the keys then sees empty entries. - Expecting negative counts.
Countersubtraction with-drops zero and negative results. Usec.subtract(other)if you need to keep negatives. - Using a deque for random access. Indexing near the middle is slower than with a list, and slicing raises an error.
- Treating namedtuple as mutable. Assignment fails. Use
_replaceor switch to a dataclass. - Forgetting maxlen drops data. A full bounded deque discards items silently, which is great for buffers but wrong for job queues that must not lose work.
When the data comes from a stream you process lazily, combine these containers with generators and the tools in our Python itertools guide. For example, Counter accepts a generator directly, so you can count lines in a large log without loading it into memory.
Final thoughts
The Python collections module is small, but it removes a surprising amount of boilerplate. Reach for Counter when you count, defaultdict when you group, deque whenever items leave from the front, and namedtuple or ChainMap when readability matters. Our timing test showed the stakes clearly: the right container turned an 11-second loop into a few milliseconds.
Frequently asked questions
Do I need to install the Python collections module?
No. It is part of the standard library, so you only need an import such as from collections import Counter.
What is the difference between dict and defaultdict?
A dict raises KeyError for a missing key. A defaultdict calls its factory function, such as list or int, to create a default value and stores it under that key.
When should I use deque instead of list?
Use deque when you add or remove items at the front, as in queues and sliding windows. In our test, draining 100,000 items from the front took about 11 seconds with a list and 3 milliseconds with a deque.
Is OrderedDict still needed in Python 3.7 and later?
Less often, because dicts keep insertion order. It is still useful for move_to_end, popitem from either end and order-sensitive equality checks.
Should I use namedtuple or a dataclass?
Use namedtuple for small, immutable records that should behave like tuples. Use a dataclass when you need mutable fields, defaults, methods or validation.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.