Skip to content

Dict Methods

Dict methods are pure. Methods with mutating Python names return a new dict instead of changing the receiver.

Method Returns Notes
d.keys() list Keys in insertion order.
d.values() list Values in insertion order.
d.items() list Two-item key/value tuples in insertion order.
d.copy() dict Shallow copy.
d.clear() dict Empty dict.
d.get(key) value | err Existing value, otherwise KeyError/NLK4003.
d.get(key, default) value Existing value, otherwise default.
d.pop(key) tuple[value, dict] | err Removed value and new dict, or KeyError/NLK4003.
d.pop(key, default) tuple[value, dict] Default and copied dict when absent.
d.set(key, value) dict Dict with key set to value.
d.remove(key) dict | err Dict without key, or KeyError/NLK4003.
d.update(other) dict Dict updated from a dict or list/tuple of pairs.

Keys must be hashable. Updating an existing key replaces its value while preserving the key position. A new key is appended. Removing and then setting a key appends it at the end. update processes incoming entries in their iteration order: existing keys retain their positions and new keys appear at the end in first-seen order. Dict equality ignores insertion order.

items() returns a list of two-item tuples. Methods that look up, set, or remove keys require hashable keys. Use key in d and key not in d for membership.

pop(key) and remove(key) return KeyError/NLK4003 when the key is absent. update() accepts a dict or a list/tuple of two-item lists or tuples.

Raw dictionary indexing is fatal on a missing key. One-argument get(key) is the safe value-level alternative and returns KeyError/NLK4003; two-argument get(key, default) returns the default without a missing-key effect. A stored error returned by either form preserves its error identity and provenance.

pop(key, default) returns (default, copy) when the key is absent. update() rejects pair inputs unless every item is a two-item list or tuple.

counts = {"a": 1}
next_counts = counts.set("b", 2)
print(next_counts.keys())
print(next_counts.values())
value, rest = next_counts.pop("a") catch (0, next_counts)
print(value)
print(rest)
print(rest.update([["c", 3]]))
Run in playground