Dictionary Methods Reference
Safe lookups, key extractions, and value assignments on dicts.
Get value for key safely, returning a default if not found.
Used In: Safe config loading, API response parsing.
dict.get(key, default=None)d = {'id': 1}
print(d.get('name', 'Unknown'))'Unknown'Time Complexity: O(1)
Common Mistakes: Confusing dict.get('key') returning None with a key that is present but holds None.
Related Methods: setdefault(), keys()
dict[key] vs dict.get(key): dict[key] raises KeyError if key is missing; dict.get(key) returns a default value safely.
Return a dynamic view object of all dictionary keys.
Used In: Membership iterations, checking keys schemas.
dict.keys()d = {'a': 1, 'b': 2}
print(list(d.keys()))['a', 'b']Time Complexity: O(1)
Related Methods: values(), items()
Return a dynamic view object of all dictionary values.
Used In: Summing counts, checking status lists.
dict.values()d = {'a': 1, 'b': 2}
print(list(d.values()))[1, 2]Time Complexity: O(1)
Related Methods: keys(), items()
Return a dynamic view object of (key, value) tuple pairs.
Used In: Looping dictionary properties in ETL data normalization.
dict.items()d = {'a': 1}
for k, v in d.items():
print(k, v)a 1Time Complexity: O(1)
Related Methods: keys(), values()
Update dictionary with keys/values from another dict or key-value pairs.
Used In: Merging config dictionaries, combining metadata feeds.
dict.update([other])d = {'a': 1}
d.update({'b': 2})
print(d){'a': 1, 'b': 2}Time Complexity: O(K) where K is items count in other
Related Methods: union operator (|)
Remove specified key and return its corresponding value.
Used In: Extracting and deleting fields from JSON payloads.
dict.pop(key[, default])d = {'id': 1}
v = d.pop('id')
print(v, d)1 {}Time Complexity: O(1)
Related Methods: popitem()
Remove and return the last inserted (key, value) pair.
Used In: Destructive buffer consumption.
dict.popitem()d = {'a': 1, 'b': 2}
k, v = d.popitem()
print(k, v)'b' 2Time Complexity: O(1)
Related Methods: pop()
Get key value if present; else set key to default and return default.
Used In: Grouping records, indexing items.
dict.setdefault(key, default=None)d = {}
d.setdefault('errors', []).append('OOM')
print(d){'errors': ['OOM']}Time Complexity: O(1)
Common Mistakes: Calling dict.setdefault(key, []) evaluates the default list [] on every call, although it is only assigned if the key is missing.
Related Methods: collections.defaultdict
dict.setdefault() vs collections.defaultdict: setdefault() is built-in but evaluates default parameters immediately; defaultdict lazily calls its factory function only when key is missing.
Remove all items from the dictionary.
Used In: Clearing global lookup tables between batches.
dict.clear()d = {'a': 1}
d.clear()
print(d){}Time Complexity: O(1)
Return a shallow copy of the dictionary.
Used In: Isolating parameter configurations.
dict.copy()d = {'a': [1]}
dup = d.copy()
dup['a'].append(2)
print(d){'a': [1, 2]}Time Complexity: O(N)
Related Methods: copy.deepcopy()