
June 30, 2023
1:50 a.m.
A dict method to retrieve the key of a value from a bijective dict would have come in handy to me in several occasions:
names = {'one': 1} names.inverse()[1] 'one' names = {'one': 1, 'uno': 1} names.inverse()[1] ValueError: dict is not bijective
My usual use case is when both keys and values have simple types like int/str, like: { 'account-a': 123, 'account-b': 456, } Users may enter the account name, or the ID. The system works with the account ID (translating from the account name if needed), but when it needs to mention the account to the user, it shows the account name instead. I do understand that retrieval wouldn't be O(1).