0

I have a set (potentially a very large number) of values generated at random, which I need to have quick access to and so from both sides. meaning:

take a tuple:

(133: 42)

one way:

getIDfromValue(42)
>>>133

the other:

getValuefromID(133)
>>>42

the way i have been doing this so far is by making two dictionnaries and accessing one or the other depending the need. like:

values = {42:133, ...}
ids = {133:42, ...}

values[42]
>>>133
ids[133]
>>>42

but that doesn't feel quite nor right nor efficient. how would one do this?

(subsidiary question: is a argument of dictionary accessed directly in memory, or is the dictionary iterated?)

thanks a lot

Max Bethke
  • 286
  • 1
  • 2
  • 18
  • Also [Efficient bidirectional hash table in Python](https://stackoverflow.com/questions/3318625/efficient-bidirectional-hash-table-in-python) – pault Apr 23 '18 at 18:57
  • Can ids and values overlap? – bphi Apr 23 '18 at 18:57
  • You could either use the bidirectional solution @pault provided or look over some of the data structures in the [collections](https://docs.python.org/3.6/library/collections.html) part of the standard library. If you truly need a bidirectional hash table then use pault's link, but you might be able to reformat the problem to work with an existing data structure. – Grant Williams Apr 23 '18 at 19:03
  • these are great references, which I didn't find before. thanks a lot. in my understanding of these dict subclassing examples, they always involve the creation of a sub-dictionary. hence, the main gain in using such structure is readability. isn't it? isn't it very similar to my two dictionaries solution, only encapsulated? – ChienMouille Apr 23 '18 at 19:27
  • @bphi sorry, what do you mean by overlap? – ChienMouille Apr 23 '18 at 19:51

0 Answers0