What I've done in the past is created a reversedict
function, which would take a dict and return the opposite mapping, either values to keys if I knew it was one-to-one (throwing exceptions on seeing the same value twice), or values to lists of keys if it wasn't. That way, instead of having to construct two dicts at the same time each time I wanted the inverse look-up, I could create my dicts as normal and just call the generic reversedict
function at the end.
However, it seems that the bidict solution that Jon mentioned in the comments is probably the better one. (My reversedict
function seems to be his bidict's ~
operator).