Dictionaries with more than one key per value in Python
- by nickname
I am attempting to create a nice interface to access a data set where each value has several possible keys. For example, suppose that I have both a number and a name for each value in the data set. I want to be able to access each value using either the number OR the name.
I have considered several possible implementations:
Using two separate dictionaries, one for the data values organized by number, and one for the data values organized by name.
Simply assigning two keys to the same value in a dictionary.
Creating dictionaries mapping each name to the corresponding number, and vice versa
Attempting to create a hash function that maps each name to a number, etc. (related to the above)
Creating an object to encapsulate all three pieces of data, then using one key to map dictionary keys to the objects and simply searching the dictionary to map the other key to the object.
None of these seem ideal. The first seems ugly and unmaintainable. The second also seems fragile. The third/fourth seem plausible, but seem to require either much manual specification or an overly complex implementation. Finally, the fifth loses constant-time performance for one of the lookups.
In C/C++, I believe that I would use pointers to reference the same piece of data from different keys.
I know that the problem is rather similar to a database lookup problem by a non-key column, however, I would like (if possible), to maintain the approximate O(1) performance of Python dictionaries.
What is the most Pythonic way to achieve this data structure?