Since you say that most requests will be resolved by looking at the first dict, the quickest solution would be to do something like:
try: item = d1[key] except KeyError: try: item = d2[key] except KeyError: ...
However, this is certainly not the most convenient form of solutions, and I do not recommend using it. You can create a function:
def get_from(item,dicts): for d in dicts: try: return d[item] except KeyError: pass else: raise KeyError("No item in dicts")
which you would call the following:
get_from(key,(d1,d2,d3))
(This is a simplified, slightly less clean version of the already very simple Chained map recipe suggested by @MartijnPieters in the comments on the original question - I would recommend using this over this code posted here. to demonstrate the concept in a more simplified way.)
Finally, perhaps a hybrid solution will work best in practice. The coefficient of the first try from the loop is a little ugly, but in most cases it avoids the loop overhead. Only if the first try raises a KeyError , do you introduce a loop type solution that I suggested above on the rest of the dicts. eg:.
try: item = d1[key] except KeyError: item = get_from(key,(d2,d3))
again, just do it if you can reliably demonstrate (think timeit ) that it makes a noticeable difference
It is important to know that python try cheap, but except worth a decent amount of time. If your code succeeds, use try - except . If this is not warranted, it is often better to use try-except in any case, but in this case you should evaluate whether performance is really a problem, and only if you can demonstrate that it is a problem, you should resort to "search before jumping" .
Last remark. If the dictionaries are relatively static, it might be worth combining them into a 1 dict :
d1.update(d2) d1.update(d3)
Now you can just use d1 - it has all the information from d2 and d3 . (of course, the order of updates matters if dicts have the same keys, but have different meanings).