1:1 mentoring with Big Tech AI engineers
Python

Python Collections

Python collections module deep dive: Counter, defaultdict, OrderedDict, namedtuple, and deque with interview examples.

Last updated

09

Python Collections — Complete Reference

Master these 10 data structures. Know when to reach for each one without thinking.

dict — The Swiss Army Knife

# O(1) avg lookup, insertion, deletion. Ordered since 3.7.
user = {"name": "Alice", "role": "engineer"}
user.get("team", "unknown")           # safe access with default
user.setdefault("team", "platform")   # set if missing, return value
{**user, "level": "senior"}           # merge (Python 3.9+: user | other)

defaultdict — Auto-initializing dict

Related

More in Python

Get full access to all 87+ sections with code examples, diagrams, and interactive animations.

Unlock Premium