A Bidirectional Dictionary

medium
Unit 21 · Immutability0/3 to milestone

Implement BiDict, a mapping where values are unique and you can look up either way:

  • b[key] = value, b[key], del b[key], len(b), key in b, iteration over keys
  • b.inverse[value] returns the key; b.inverse is itself a BiDict view that stays in sync (setting b.inverse[v] = k also updates b)
  • assigning a value that already belongs to another key raises ValueError; re-assigning a key to a new value removes the old value from the inverse

Subclass collections.abc.MutableMapping so you get get, items, update and friends for free.

#dictionaries#data-structures2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
A Bidirectional Dictionary · TechGradify