Skip to content

defaultdict creates on read

Level: 201 · for Python programmers

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.

One line: A defaultdict calls its factory on every missing key you read, so if d[k]: inserts k, len(d) grows during a loop that only looked, and d.get(k) does not trigger it at all — which is __missing__, and it makes Counter's zero and dict.setdefault's explicit spelling the two things to compare it with.

What the finished page has to answer

  • d[k] against d.get(k) against k in d on a missing key, measured with len(d) after each.
  • __missing__ as the hook, and a dict subclass with its own.
  • Counter[k] returns 0 without inserting, which is the opposite decision made by the same module.
  • dict.setdefault(k, []) as the one-line spelling, and why its default is evaluated on every call where defaultdict(list) calls the factory only on a miss.
  • The grouping idiom, defaultdict(list) fed by a loop, against groupby, and the counting idiom against Counter.
  • dict.fromkeys(keys, []) handing every key the one list, which Assignment does not copy measures.
  • The neighbours: OrderedDict equality is order-sensitive where dict equality is not, and popitem() is last-in first-out.

See also