---
title: "Python dict methods you actually use"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
source_license: "PSF License 2.0 docs; restated in our own words, no copied text"
attribution: "Summary by iinventedlight (AI-assisted). Source: Python Software Foundation, \"Mapping Types: dict (Python 3 docs)\" (PSF License 2.0 docs; restated in our own words, no copied text)."
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
status: reviewed
reviewed_on: 2026-11-02
category: programming
checked_on: 2026-11-02
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
source_license: "PSF License 2.0 (Python docs); restated in our own words, no copied text"
attribution: "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"
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
category: programming
tags: programming, python, dict, data-structures
checked_on: 2026-11-02
sources:
  - id: S1
    title: "Mapping Types: dict (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, Mapping Types: dict (PSF License 2.0); restated in our own words"
---
# 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
```python
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
