← Reference notes

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

Caveats

Pointers

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

Permanent ID: iil:note:2shj751x

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.