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)})
  1. 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'}m
  2. inv ← {}

    1m = {'r': 'red', 'g': 'green', 'b': 'blue', 'y': 'yellow', 'w': 'white'}2inv = {}3for k, v in m.items():
    values this step{}inv
  3. k ← 'r', v ← 'red'

    2inv = {}3for k, v in m.items():4    inv[v] = k
    values this step'r'k'red'v
  4. inv ← {'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'}inv
  5. k ← 'g', v ← 'green'

    2inv = {}3for k, v in m.items():4    inv[v] = k
    values this step'r' 'g'k'red' 'green'v
  6. inv ← {'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'}inv
  7. k ← 'b', v ← 'blue'

    2inv = {}3for k, v in m.items():4    inv[v] = k
    values this step'g' 'b'k'green' 'blue'v
  8. inv ← {'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'}inv
  9. k ← 'y', v ← 'yellow'

    2inv = {}3for k, v in m.items():4    inv[v] = k
    values this step'b' 'y'k'blue' 'yellow'v
  10. inv ← {'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'}inv
  11. k ← 'w', v ← 'white'

    2inv = {}3for k, v in m.items():4    inv[v] = k
    values this step'y' 'w'k'yellow' 'white'v
  12. inv ← {'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'}inv
  13. for k, v in m.items():

    2inv = {}3for k, v in m.items():4    inv[v] = k
  14. stdout ← 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 to inv, not m.