I have 20 strings which match 20 integers. All integers and strings are unique. I am considering but would like to avoid creating two dictionaries. One dictionary will be indexed by the string, and one dictionary will be indexed by the integer.
- How should I handle this situation?
I created two lists. One containing strings, the other containing integers. I am thinking of building two functions. One function will produce a string. The other function will produce an integer. Another alternative is to combine these into one function by branching if produced argument is an integer or a string.
- How is this comparable to a dictionary? Will it consume a lot of cpu?
(this function will run millions of times everyday) - Should I just create a list of tuples which are (string, int) and then
create two dictionaries, one mapping int to list position and other
string to list position? Will it be the best way?
I don’t have many items so I can sacrifice some memory.
Please explain the best method with an explanation of why it is the best.
Thank you.
Whatever solution you go with, if you want it to be robust, you should probably wrap a class around it that automatically updates the other direction when you update one. For instance, here’s a start at a basic bidirectional dictionary using @mgilson’s technique (which means it won’t work if there’s any overlap between the two sets of items you’re mapping to each other; having different types works great, though):
You can use it like this: