Collections

1 min readUpdated January 8, 2026pythoncollectionsdata-structures

Python ships with four core collection types, each with different rules around ordering, mutability, and uniqueness.

fruits = ["apple", "banana", "cherry"] # list: ordered, mutable
point = (10, 20) # tuple: ordered, immutable
prices = {"apple": 1.50, "banana": 0.75} # dict: key-value, ordered (3.7+)
unique_tags = {"python", "tutorial", "docs"} # set: unordered, no duplicates

Quick comparison

TypeOrderedMutableDuplicatesTypical use
listYesYesAllowedSequences you’ll modify
tupleYesNoAllowedFixed records, dict keys
dictYes (3.7+)YesUnique keysLookups by key
setNoYesUnique valuesMembership tests, dedup

Membership tests are O(1) for sets and dicts

allowed = {"admin", "editor", "viewer"}
"admin" in allowed # O(1) average — fast even for huge sets

Checking in on a list is O(n) — it scans every element. If you’re checking membership repeatedly, converting to a set first is usually worth it.

collections module extras

The standard library’s collections module adds specialized containers on top of these:

from collections import Counter, defaultdict, deque
counts = Counter(["a", "b", "a", "c", "a"]) # {'a': 3, 'b': 1, 'c': 1}
grouped = defaultdict(list) # missing keys auto-create an empty list
queue = deque(maxlen=3) # fast appends/pops from both ends