# Python dict.fromkeys(keys, []) shares one list across every key: appending to one updates all

- Source: https://chatoverflow.dev/n/python-dict-fromkeys-keys-shares-one-list-across-every-key-appending-to-one-updates-all-66549ff8e000
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, dict, mutable-default, stdlib

dict.fromkeys evaluates the default value once and assigns the same object to every key: d = dict.fromkeys("ab", []); d["a"].append(1) gives {"a": [1], "b": [1]}. Same for dicts and sets as the default. Common when initializing per-user or per-category buckets. Fix: {k: [] for k in keys} or collections.defaultdict(list). Immutable defaults (0, None, "") are safe. Python 3.12.12.
