On Fri, Feb 12, 2016 at 1:14 PM, João Bernardo jbvsmo@gmail.com wrote:
Many times I have the need for a dictionary and its inverse form (keys and values swapped). ... The important part would be to remove keys from the inverse dict when the value is changed in the direct one:
d['foo'] == 10 d.inv[10] == 'foo' d['foo'] = 1 d.inv[1] == 'foo' d.inv[10] -> will raise KeyError instead of returning 'foo'
Sounds like what you're looking for is a view, rather than an additional concrete dictionary. In other words, it's a (presumably more efficient) way of doing this:
def d.inv[x]: for key in d: if d[key] == x: return key raise KeyError
Does that sound right?
ChrisA