Learn collections

Specialised containers: Counter, defaultdict, deque, namedtuple and more

13 lessons · 2 hours · 4 levels · covers collections Python 3.13

The collections module adds high-performance container types to Python's built-in list, dict, set and tuple: Counter for tallies, defaultdict for grouping, deque for queues, namedtuple for lightweight records, OrderedDict and ChainMap for ordering and layering. Its companion collections.abc defines the abstract interfaces of all containers. Using the right container makes code shorter and often much faster.

Before you start

collections builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 32 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The collections tree

collectionsCounting with CounterCounting withCounterGrouping with defaultdictGrouping withdefaultdictRecords with namedtupleRecords withnamedtupleFast queues with dequeFast queues withdequeCounter arithmetic and multisetsCounter arithmeticand multisetsdeque patterns: windows, BFS, rotationdeque patterns:windows, BFS, rota…OrderedDict and LRU cachesOrderedDict and LRUcachesDictionariesDictionariesTuples and unpackingTuples andunpackingListsLists
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Counting and grouping

  1. Counting with Counter · Essentials · 8 min

    Tally hashable items with Counter and get the most common ones.

  2. Grouping with defaultdict · Essentials · 8 min

    Let missing keys get an automatic default value, the idiomatic way to group items into lists or sets.

  3. Counter arithmetic and multisets · Practical · 9 min

    Combine and compare Counters with +, -, &, |, subtract and comparison operators, treating them as multisets.

Queues and records

  1. Records with namedtuple · Essentials · 8 min

    Create lightweight, immutable record types whose fields are accessible by name and by position.

  2. Fast queues with deque · Essentials · 8 min

    Use deque for O(1) appends and pops at both ends, and maxlen for bounded history.

  3. deque patterns: windows, BFS, rotation · Practical · 10 min

    Apply deque to breadth-first search, sliding windows, round-robin scheduling with rotate, and tail -n.

  4. namedtuple vs NamedTuple vs dataclass · Advanced · 10 min

    Choose between collections.namedtuple, typing.NamedTuple and dataclasses for record types, knowing the trade-offs.

Mapping variants

  1. OrderedDict and LRU caches · Practical · 9 min

    Know what OrderedDict still adds over insertion-ordered dicts: move_to_end, popitem(last=False) and order-sensitive equality.

  2. Layered lookups with ChainMap · Advanced · 9 min

    Search several dicts as one with ChainMap, for configuration layers and nested scopes.

Abstract base classes

  1. collections.abc: container interfaces · Advanced · 12 min

    Use the abstract base classes in collections.abc to check capabilities and to build full containers from a few methods.

  2. Subclassing dict vs UserDict · Expert · 11 min

    Understand why overriding methods on a dict subclass is unreliable, and when to use UserDict, MutableMapping or __missing__ instead.

Internals and concurrency

  1. How deque, Counter and dicts are implemented · Expert · 11 min

    Know the data structures and complexity behind deque, Counter, defaultdict and OrderedDict, to predict performance.

  2. Containers and thread safety · Expert · 10 min

    Which deque and dict operations are atomic, why Counter increments race, and how this changes on free-threaded Python 3.13.