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. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

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)

Common patterns and gotchas

Safe lookups without KeyError

The cleanest way to avoid a KeyError depends on whether you want to insert a default at the same time:

# Just look up, return None if missing — never inserts
value = d.get("key")
value = d.get("key", 0)   # with a fallback value

# Look up AND insert if missing
count = d.setdefault("key", 0)
d["key"] += 1  # works reliably

The fromkeys trap

dict.fromkeys(keys, mutable_value) shares a single mutable object across all keys:

# Danger: all keys share one list
bad = dict.fromkeys(["a", "b", "c"], [])
bad["a"].append(1)   # bad["b"] and bad["c"] also become [1]

# Fix: use a comprehension
good = {k: [] for k in ["a", "b", "c"]}

Merging dicts in different Python versions

# Python 3.9+ — creates a new merged dict
merged = first | second

# Python 3.9+ — update first in place
first |= second

# Python 3.5+ — spread syntax (use in dict literal)
merged = {**first, **second}

# All versions
first.update(second)  # mutates first in place

View objects and iteration

keys(), values(), and items() return view objects that track the live dict state. They are safe to read and support in membership tests in O(1) for keys/items. The constraint is that you cannot modify the dict while iterating a view — doing so raises RuntimeError:

for key in list(d.keys()):   # snapshot with list() if you need to delete during iteration
    if should_remove(key):
        del d[key]

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.