Python dict methods you actually use
Python dict methods you actually use
Summary
Python dicts map keys to values and, since Python 3.7, keep insertion order. Use d.get(k, default) to read a key without risking a KeyError, setdefault to read or insert, pop and popitem to remove, and update or the | operator (3.9+) to merge. keys(), values() and items() return live views.
Draft. License: CC BY 4.0 (this summary). Sources: [S1] Python Software Foundation, "Mapping Types: dict (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text).
Example
prices = {"apple": 1.2}
prices.get("pear", 0) # 0, no KeyError
prices.setdefault("pear", 0.8) # inserts pear
merged = prices | {"apple": 1.0} # 3.9+
for k, v in prices.items():
print(k, v)
Facts
- id: F1 claim: d[key] raises KeyError if the key is missing; d.get(key, default) returns default (None if omitted) instead and does not insert the key. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F2 claim: d.setdefault(key, default) returns the value if key exists; otherwise it inserts key with default and returns default. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F3 claim: d.pop(key[, default]) removes and returns the value; without a default it raises KeyError for a missing key. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F4 claim: d.popitem() removes and returns a (key, value) pair in LIFO order (guaranteed since 3.7); it raises KeyError on an empty dict. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F5 claim: Dictionaries preserve insertion order, a language guarantee since Python 3.7. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F6 claim: keys(), values() and items() return view objects that reflect later changes to the dict. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F7 claim: d1 | d2 (Python 3.9+) returns a new merged dict with d2's values winning on shared keys; d1 |= d2 updates in place, like d1.update(d2). confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F8 claim: dict.fromkeys(iterable, value) gives every key the same value object, so a mutable value such as a list is shared across all keys. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F9 claim: Keys must be hashable; lists cannot be keys, tuples of hashable items can. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
Caveats
- For counting or grouping, collections.Counter and collections.defaultdict are often cleaner.
Pointers
- id: P1 answers: Full details and updates where: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict access: free, public web page
Source: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict · License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted), condensed and paraphrased; sources: Python Software Foundation, Python 3 documentation, Mapping Types: dict (PSF License 2.0); restated in our own words
Cite this
iinventedlight Library. 2026. "Python dict methods you actually use." iinventedlight Library, iil:note:2shj751x, version of 2026-11-02. https://iinventedlight.com/library/reference/python-docs/python-dict-methods/. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/stdtypes.html#mapping-types-dict.
@misc{iil_note_2shj751x,
author = {{iinventedlight Library}},
title = {{Python dict methods you actually use}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-02},
url = {https://iinventedlight.com/library/reference/python-docs/python-dict-methods/},
note = {Permanent ID iil:note:2shj751x. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/stdtypes.html\#mapping-types-dict.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/reference/python-docs/python-dict-methods.md.