> On 30 Jun 2023, at 02:50, Andre Delfino <adelf...@gmail.com> wrote:
> 
> 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).

In cases like this I have two dict one for each look up direction.

by_name = {}
by_id = {}

def add(name, id):
        by_name[name] = id
        by_id[id] = name

etc.

Barry


> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/BWMIASE5YCDETFSZYAUG7TVWTMBFHKQW/
> Code of Conduct: http://python.org/psf/codeofconduct/
> 

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NG6QFBDGHCJXKUFHCONO2U545WLI6N5F/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to