ML Foundations

Python dict views stay live; lists are snapshots

dict.keys(), values(), and items() return dynamic views. Materialize a list when you need the entries captured at one point in time.

Example results for Python dict views stay live; lists are snapshots. live view after mutation: ('alpha', 1), ('beta', 2), ('gamma', 3)
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A dictionary view refers to the mapping

The objects returned by `dict.keys()`, `dict.values()` and `dict.items()` are not lists of entries frozen at call time. They are dynamic views of the dictionary. If the mapping changes, a later iteration of the view reflects the changed entries. [1]

That behavior is convenient when code should observe the current mapping without building a separate collection. It can surprise code that stores `items = mapping.items()` and expects the result to remain unchanged after another function mutates the dictionary. The variable holds a view, not a snapshot. [1]

References: [1]

Materialize when you need a point-in-time collection

The example captures `list(records.items())`, then adds a third key. Iterating the existing view shows all three pairs, while the list remains the two pairs it contained when created. The assertions verify both behaviors and make the distinction executable. [1]

A shallow `dict.copy()` is another way to capture the mapping structure at that time, although nested mutable values are still shared. A list of items captures the key/value pairs but likewise does not deep-copy objects referenced by those values. Choose the snapshot form that matches whether you need iteration order, mapping lookup, or independent nested data. [2]

Executed with Python 3.12.14; assertions passed.

"""Compare a live dictionary view with a shallow snapshot."""
records = {"alpha": 1, "beta": 2}
view = records.items()
snapshot = list(records.items())
records["gamma"] = 3

print("live view after mutation:", list(view))
print("snapshot after mutation:", snapshot)
print("view length:", len(view))

assert len(view) == 3
assert ("gamma", 3) in view
assert snapshot == [("alpha", 1), ("beta", 2)]

Recorded output

live view after mutation: [('alpha', 1), ('beta', 2), ('gamma', 3)]
snapshot after mutation: [('alpha', 1), ('beta', 2)]
view length: 3

References: [1], [2]

Avoid structural mutation during an active iteration

A live view reflects changes, but changing a dictionary size while iterating through the view can raise `RuntimeError` or lead to an incomplete traversal. If the loop must remove or add keys, iterate over a materialized list of the relevant keys or items, then mutate the dictionary separately. [1]

The sample mutates before iterating, which demonstrates the view relationship without changing the dictionary during an active traversal. It covers one shallow dictionary with immutable integer values; it does not test concurrent threads, nested mutable values or mutation of values without changing dictionary size. [1, 2]

References: [1], [2]

Sources

  1. Python: dictionary view objects
  2. Python: dict.copy