0

What would be the fastest, most efficient way to grab and map multiple values to one value. For a use case example, say you are multiplying two numbers and you want to remember if you have multiplied those numbers before. Instead of making a giant matrix of X by Y and filling it out, it would be nice to query a Dict to see if dict[2,3] = 6 or dict[3,2] = 6. This would be especially useful for more than 2 values.

I have seen an answer similar to what I'm asking here, but would this be O(n) time or O(1)? print value for matching multiple key

for key in responses:
    if user_message in key:
        print(responses[key])

Thanks!

1 Answers1

0

Seems like the easiest way to do this is to sort the values before putting them in the dict. Then sort the x,y... values before looking them up. And note that you need to use tuples to map into a dictionary (lists are mutable).

the_dict = {(2,3,4): 24, (4,5,6): 120}
nums = tuple(sorted([6,4,5]))
if nums in the_dict:
    print(the_dict[nums])
Ed OBrien
  • 76
  • 4