Say I have a dictionary as follows:
example_dict = {"foo": 1, "bar": 2}
And I want to do multiple lookups semantically equivalent to:
foo = example_dict["foo"]
bar = example_dict["bar"]
Is there any way of doing multiple lookups at once? The closest I have got is using a list comprehension or a generator expression, like:
foo, bar = [example_dict[key] for key in ["foo", "bar"]]
foo, bar = (example_dict[key] for key in ["foo", "bar"])
Ideally what I'd like to do is something like:
foo, bar = example_dict.getmany(["foo", "bar"])
Or even:
foo, bar = example_dict["foo", "bar"]
In a similar way to what you would do with a list or tuple:
foo, bar = "foo", "bar"
Obviously I could define my own function to do this by returning the list comprehension or generator expression above. Or I could create my own dictionary class which if given an iterable to look up, does a lookup per key and returns an iterable. In other data structures (i.e. not a hash), looking up several keys at once feels like it could theoretically provide better performance than many individual lookups. Is there any way to do this?