python
8 minPython Collections: Lists, Tuples, Sets, and Map/Reduce Trade-offs
Choose Python collection types by invariants and access patterns, then use map, comprehensions, and reductions without obscuring intent.
Loading page...
python
8 minChoose Python collection types by invariants and access patterns, then use map, comprehensions, and reductions without obscuring intent.
Q&A
Q&ABase the choice on ordering, mutation, duplicates, and membership operations.
A list is ordered, mutable, and permits duplicates. A tuple is an immutable sequence suited to fixed records, though named structures can improve readability. A set stores unique hashable values and is useful for membership; do not use it when output order or duplicates are part of the contract.
databases
6 minUse a unique ordering tuple and a deliberate visibility policy for reliable page traversal.
Q&A
Q&AEqual timestamps otherwise make continuation skip or repeat peers.
Carry the full ordering tuple, such as created_at and ID, and match its direction in the continuation predicate. Stable continuation still does not freeze membership across requests.
Q&A
Q&ATIME_WAIT is part of safe TCP teardown; verify actual ephemeral-port pressure before tuning sysctls.
TIME_WAIT helps prevent delayed packets from an old connection from contaminating a new one. A high count can be normal. Diagnose port exhaustion by checking outbound tuple reuse, NAT limits, ephemeral port range, and connection pooling. tcp_tw_reuse semantics vary by kernel and are not a universal first fix.
Q&A
Q&ASet members must be hashable, and a mutable list does not have a stable hash.
Sets and dictionary keys rely on a stable hash and equality relationship while a value is stored. A list is mutable, so changing it could invalidate the collection’s lookup structure; Python therefore makes lists unhashable. Use a tuple if its members are also hashable and the value should be immutable.
Q&A
Q&AA one-to-many join changes row grain; multiple child joins can multiply each other.
Check the cardinality of each join. Joining one order to several lines produces multiple rows per order; joining another one-to-many table can multiply those rows again. Aggregate each relation to the intended grain before joining or use EXISTS when you only need to test presence. DISTINCT is not a reliable repair for incorrect join logic.
Q&A
Q&ACheck active work and catalog validity before dropping or retrying.
Concurrent builds use multiple phases and can leave an invalid index. Inspect the server error and pg_index readiness/validity, waits, and transactions; do not assume the operation was atomic or load-free.