Maps, Sets, and Records
-
Looking Up a Key in a Dictionary
A dictionary connects each distinct key to a value. A lookup needs a policy for a key that has no entry.
-
Counting Frequencies in a Dictionary
A frequency map associates each observed key with the number of times it occurred.
-
Sets, Membership, and Duplicate Values
A set stores membership under an equality rule and does not retain a separate entry for every occurrence.
-
Set Union, Intersection, and Difference
Set operations combine membership questions and can express inclusion, overlap, and exclusion without a separate search for every element.
-
Finding Changed Membership with Symmetric Difference
Symmetric difference contains items present in exactly one of two sets. It detects membership changes in either direction.
-
Checking Required Membership with Subsets
A subset test verifies that every required element appears in an available set.
-
Dictionary Insertion Order and Sorting
Python dictionaries preserve insertion order. This order is useful for source history and can differ from ascending key order.
-
Dictionary Keys, Values, and Items
A dictionary view can traverse its labels, its stored values, or pairs of both. Pick a view that preserves the evidence a caller needs.
-
Using a Dictionary to Group Records
A grouping map stores one collection for each key and preserves every received record in the appropriate group.
-
Building a Reverse Index from a Mapping
A reverse mapping relates values back to their original keys. Repeated values can require multiple keys per reverse entry.
-
Merging Settings at Different Priorities
Settings often combine default, environment, and user values. Define which supplied entry wins for a shared key.
-
Live Dictionary Views and Snapshots
A live view observes changes to the same dictionary. A collected sequence of view items fixes the items observed at collection time.
-
Using Composite Keys in a Dictionary
A composite key joins several fields into one lookup identity. Every field participating in that identity needs a stable representation.
-
A Record with Named Fields
A record associates names with a defined set of fields describing one object, observation, or result.
-
Optional Fields in a Record
An omitted field and a present field carrying an empty value can be different states of a record.
-
Joining Two Collections by Key in Memory
A lookup map can associate items from two collections efficiently when join keys and multiplicities are understood.
-
A Visited Set in a Search
A visited set remembers explored states so a search can avoid repeatedly expanding the same state.
-
A Dictionary as a Simple Result Cache
A cache reuses results for equivalent calls. The chosen key has to capture every input that changes the result.
-
Ordered and Unordered Collection Equality
A list, set, and frequency map answer different equality questions about order and multiplicity.
-
Case Folding for Collection Keys
Normalize lookup keys when a domain defines a case-insensitive identity. Apply the same rule to both storage and retrieval.
-
A Queue of Work Records
A work queue can hold an identifier, input values, and enough context for each task to be handled in arrival order.
-
Building an Index Before Looking Up References
A first pass can index records, and a second can resolve references regardless of where their targets appear in the input.
-
Collection Size and Retained Memory
Element count is one part of a collection's memory use. The referenced values and spare storage can also matter.
-
Building a Categorical Histogram
A categorical histogram counts how many records fall under each label and reports the counts under a chosen order.
-
Partitioning Records into Disjoint Groups
A partition places each input item into exactly one group. A grouping rule can otherwise duplicate a row across overlapping labels.
-
Subtracting Inventories with Counts
A multiset keeps the number of occurrences of each key. A count-based difference can show unmet quantity even when a key is available.
-
Making an Immutable Snapshot of a Record
An immutable snapshot preserves selected values at one moment so later mutations elsewhere cannot silently rewrite the observation.
-
Caching a Missing Lookup Result
Caching a missing result can avoid repeated expensive work, but the application needs a policy for when absence can change.
-
Turning a Mapping into Report Rows
A report row can retain the key and value from a mapping while presenting them in an independently chosen order.
-
Choosing Stable Identifiers for Stored Records
An identifier remains attached to an entity when mutable descriptions change. It gives collections a reliable key for updates and links.