Python Dict Methods Reference

All Python dict methods with signature, return value and 3.7+ order notes

Searchable reference for every Python dict method — get, setdefault, update, pop, popitem, items, keys, values — with signature, return value, and insertion-order behavior.

Do Python dictionaries preserve insertion order?

Yes. Since Python 3.7 the language guarantees that dictionaries remember the order in which keys were inserted, and iteration over keys, values, or items follows that order. This was an implementation detail in 3.6 and became an official guarantee in 3.7. Updating an existing key does not change its position; only deleting and re-inserting moves it to the end.

Python’s dict is a mutable mapping that, since 3.7, preserves insertion order. Its methods cover safe lookup, defaults, merging, removal, and the dynamic view objects returned by keys, values, and items. This is a searchable offline reference.

How it works

Each method lists its signature, return value, and behavior:

  • Lookupget(key, default) returns a default instead of raising; indexing d[key] raises KeyError.
  • Defaultssetdefault(key, default) returns the value, inserting the default if the key is absent.
  • Mergeupdate(other) writes another mapping’s pairs in; the | operator (3.9+) returns a merged dict.
  • Removalpop(key[, default]) removes and returns a value; popitem() removes and returns the last inserted pair (LIFO since 3.7).
  • Viewskeys(), values(), items() return live views reflecting later changes; wrap in list(...) for a snapshot.

Example

Group values into lists with setdefault:

groups = {}
for name, team in members:
    groups.setdefault(team, []).append(name)

Notes

  • Insertion order is guaranteed since Python 3.7; popitem() removes the last-inserted pair.
  • View objects are dynamic — they track changes to the dict. Iterating a view while mutating the dict raises RuntimeError.
  • fromkeys(keys, value) is a classmethod; beware passing a mutable default — all keys share the same object.
  • This reference covers all standard dict methods.