I have come to a need to represent a one to one relationship in python.
The easiest thing to do is have a list of tuples. [(thing_one, thing_two]
. But this would get you O(N) translation/deletion/insertion time. Not ideal.
Next you could use two dictionaries
one_to_two
, two_to_one
. This would get you O(1) translation/insertion/deletion. But its easy to forget to update one without the other and you are doubling the amount of memory you now need to represent this relationship. You could wrap this in a class to enforce the bijection, but you still don't solve the information duplication.
Is there a good way to represent a relationship like this? Preferably with O(1) ops and no data duplication? Maybe a python module that handles this for you?