0

I have a python dictionary which uses a tuple as a dictionary key as follows:

d = {}
d[(0, 'Port')] = 0
d[(1, 'Host')] = 1

Now, in the tuple both the integer and the string are unique across the dictionary i.e. if (0, 'Port') exists than you will not have an entry (1, 'Port'). Now, I want to be able to access the value when the user provides just one part of the key (integer or the string).

Would this be possible at all without looping through all the keys?

Luca
  • 10,458
  • 24
  • 107
  • 234
  • 2
    If both values are unique in the dictionary then using 2 values is redundant. Use only one key, maybe the string and make a list of string-keys so you can access them by indexing: `['Port','Host']` – Gábor Fekete Aug 03 '16 at 10:01
  • The reason I was trying to do it like this was so that people could access the key either by the integer or by the string. Would maintaining two dictionaries that map the string to integer ID and vice versa a reasonable solution? – Luca Aug 03 '16 at 10:10
  • 2
    I think this will help you: http://stackoverflow.com/questions/10123853/how-do-i-make-a-dictionary-with-multiple-keys-to-one-value – Gábor Fekete Aug 03 '16 at 10:17
  • Ahhhh of course, create two keys! Thanks. – Luca Aug 03 '16 at 10:23

0 Answers0