You can use a dict with a key from 2 tuples, and its value as a string / any, and then you can keep only valid values โโin mind and have a default value if you want ... (using dict.get )
So, if you have a list of refs , you can convert them to a dict and search as such:
refs = [ ('1','a', 'string1'), ('1','b', 'string2'), ('1','c', 'string3'), ('1','d', 'string3'), ('2','a', 'invalid'), ('2','b', 'invalid'), ('2','c', 'string3'), ('2','d', 'string3') ] lookup = {ref[:2]:ref[2] for ref in refs} print lookup['1', 'd'] #string3 print lookup.get(('I do not', 'exist'), 'uh oh, in trouble now!') # uh oh, in trouble now!
source share