Dictionaries as Tables
Invert Mapping
Swap the keys and values of a dict to produce an inverse mapping. The trace
shows inv growing one swapped entry at a time as the loop walks the original
dict's items.
By hand
Iterate over .items() to unpack each (k, v) pair, then assign inv[v] = k
to build the reverse mapping entry by entry.
naive.py
Replay: real traced execution (multi-file project)
m = {'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}
inv = {}
for k, v in m.items():
inv[v] = k
print('RESULT:', {k: inv[k] for k in sorted(inv)})
m ← {'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}
1m = {'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}2inv = {}values this step{'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}minv ← {}
1m = {'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}2inv = {}3for k, v in m.items():values this step{}invk ← 'r', v ← 'red'
2inv = {}3for k, v in m.items():4 inv[v] = kvalues this step'r'k'red'vinv ← {'red': 'r'}
3for k, v in m.items():4 inv[v] = k5print('RESULT:', {k: inv[k] for k in sorted(inv)})values this step{} → {'red': 'r'}invk ← 'g', v ← 'green'
2inv = {}3for k, v in m.items():4 inv[v] = kvalues this step'r' → 'g'k'red' → 'green'vinv ← {'red': 'r', 'green': 'g'}
3for k, v in m.items():4 inv[v] = k5print('RESULT:', {k: inv[k] for k in sorted(inv)})values this step{'red': 'r'} → {'red': 'r', 'green': 'g'}invk ← 'b', v ← 'blue'
2inv = {}3for k, v in m.items():4 inv[v] = kvalues this step'g' → 'b'k'green' → 'blue'vinv ← {'red': 'r', 'green': 'g', 'blue': 'b'}
3for k, v in m.items():4 inv[v] = k5print('RESULT:', {k: inv[k] for k in sorted(inv)})values this step{'red': 'r', 'green': 'g'} → {'red': 'r', 'green': 'g', 'blue': 'b'}invk ← 'y', v ← 'yellow'
2inv = {}3for k, v in m.items():4 inv[v] = kvalues this step'b' → 'y'k'blue' → 'yellow'vinv ← {'red': 'r', 'green': 'g', 'blue': 'b', 'yellow': 'y'}
3for k, v in m.items():4 inv[v] = k5print('RESULT:', {k: inv[k] for k in sorted(inv)})values this step{'red': 'r', 'green': 'g', 'blue': 'b'} → {'red': 'r', 'green': 'g', 'blue': 'b', 'yellow': 'y'}invk ← 'w', v ← 'white'
2inv = {}3for k, v in m.items():4 inv[v] = kvalues this step'y' → 'w'k'yellow' → 'white'vinv ← {'red': 'r', 'green': 'g', 'blue': 'b', 'yellow': 'y', 'white': 'w'}
3for k, v in m.items():4 inv[v] = k5print('RESULT:', {k: inv[k] for k in sorted(inv)})values this step{'red': 'r', 'green': 'g', 'blue': 'b', 'yellow': 'y'} → {'red': 'r', 'green': 'g', 'blue': 'b', 'yellow': 'y', 'white': 'w'}invfor k, v in m.items():
2inv = {}3for k, v in m.items():4 inv[v] = kstdout ← RESULT: {'blue': 'b', 'green': 'g', 'red': 'r', 'white': 'w', 'yellow': 'y'}
4 inv[v] = k5print('RESULT:', {k: inv[k] for k in sorted(inv)})values this stepRESULT: {'blue': 'b', 'green': 'g', 'red': 'r', 'white': 'w', 'yellow': 'y'}stdout
The Pythonic way
A dict comprehension over .items() expresses the swap in one line: swap
the roles of k and v in the key-value assignment.
library.py
m = {'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}
inv = {v: k for k, v in m.items()}
print('RESULT:', {k: inv[k] for k in sorted(inv)})
RESULT: {'blue': 'b', 'green': 'g', 'red': 'r', 'white': 'w', 'yellow': 'y'}
Implementation notes
- Inversion is only well-defined when values are unique. If two keys share
the same value, the loop silently overwrites the earlier entry and only the
last key survives in
inv— no error is raised. - The RESULT dict is built via
{k: inv[k] for k in sorted(inv)}so the printed order is alphabetical by the original values (now keys), regardless of iteration order. m.items()returns a view, not a copy; it is safe to iterate and read from simultaneously because the loop body only writes toinv, notm.