[Python-ideas] dict method to retrieve a key from a value

2023-06-29 Thread Andre Delfino
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 key

[Python-ideas] dict method to retrieve a key from a value

2023-06-29 Thread Stephen J. Turnbull
Andre Delfino writes: > A dict method to retrieve the key of a value from a bijective dict > would have come in handy to me in several occasions: [...] > I do understand that retrieval wouldn't be O(1). If O(log N) is good enough and bijectivity is guaranteed by some other mechanism, a bisecti

[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-29 Thread Christopher Barker
I've had use cases for this, and written a class to do it (using Stephen's two dict approach. I'd be really surprised if there wasn't one on PyPi --the trick is to know what to call it to search for it. Now that I think about it -- my use case was many-to-many, not one to one -- so not quite the s

[Python-ideas] Re: dict method to retrieve a key from a value

2023-06-29 Thread Stephen J. Turnbull
Christopher Barker writes: > > If O(log N) is good enough and bijectivity is guaranteed by some > > other mechanism, a bisection search on d.items() with > > key=lambda x: x[1] does the trick. > You'd also have to keep it sorted by value. I assumed you can do that with OrderedDict. But yeah