Data Structures Highlights Summary
Every highlight bullet from the 11 pages in this section, gathered on one page and grouped by the page it came from.
- Every built-in container is either an array-backed sequence or a hash-table-backed mapping/set
- Big-O differences between containers come from implementation, not from the interface you call
- collections.abc names the protocols (Iterable, Sized, Sequence, Mapping) containers implicitly satisfy
- dict and set share the same hash-table engine; list and tuple share the same contiguous-array layout
- Pick containers by access pattern, not habit
- dict preserves insertion order in Python 3.7+
- set gives O(1) average membership tests
- tuple is immutable and hashable when contents are
- Lists mutate in place; tuples do not
- sort() is in-place; sorted() returns new list
- bisect maintains sorted lists for O(log n) insert
- Unpacking works on any iterable sequence
- dict preserves insertion order by language spec
- keys must be hashable and immutable-ish
- | merge operator updates without mutating (3.9+)
- views reflect live dict changes
- set provides O(1) average membership
- Literals need at least one element or set()
- frozenset is hashable for nested keys
- Operators mirror math set notation
- defaultdict removes repetitive key checks
- Counter is a multiset with arithmetic
- deque is the right queue primitive
- OrderedDict rarely needed since 3.7 dict order
- heapq is a min-heap on a plain list
- Push tuples to break priority ties
- heapreplace for streaming top-K
- nlargest/nsmallest beat sort for small k
- array.array stores C numeric types compactly
- bytes is immutable; bytearray is mutable
- memoryview enables zero-copy buffer slicing
- struct packs binary protocol fields
- Start from access patterns, not familiarity
- dict/set win on membership at scale
- deque beats list for queue workloads
- Reach for pandas/polars at tabular scale
- Hashable objects need stable hash and equality
- Mutable containers are not hashable
- frozen dataclass enables hashable records
- Custom classes need eq and hash aligned
- Match container to access pattern
- Avoid list.pop(0) for queues
- dict.fromkeys preserves dedupe order
- Do not use mutable objects as dict keys
Revisado por Chris St. John·Última actualización: 31 jul 2026