This looks like a X, Y problem. -
You may want a “deconstructor” - a shortcut to retrieve multiple values from a dictionary into local names
update\] - sorry, if you want a subset of the original dictionary, not a deconstructor, then the language has a construct for it: dict comprehensions\[/update
For one, the feature that was added to the language to do this kind of thing was the match/case constrcut. Unfortunatelly, it ended up being too verbose and bloated for this use case - it works mostly as a normalizer for a single item.
Now, most important - why this is a backwards-compatibility “no go”:
Then, passing a non-hashable object, like a list, as the index operator in the `a[b]` syntax errors in dictionaries, but only because default dictionaries don’t take lists as keys - it is perfectly legal and valid syntax, and works for any object that implements `_getitem_`.
While it on the surface looks like “ok, but this would be a feature for dictionaries only, other mappings still do as they please”, a “dict” is what “de facto” ditctates the expected interface for a mapping in Python. Changing the way dicts handle one specific kind of object as key would break the expectation that anything currently implementing `collactions.abc.Mapping` can be used as a drop-in replacement for a dict - that is hundreds, more likely thousands, of implementations of classes in popular existing packages.
Workaround for personal/in house projects:
For one, if the proposed syntax would make sense in your projects, it is a matter of creating a dictionary wrapper to do that, and you are good:
from collections import UserDict
class DeconstructorDict(UserDict):
def __getitem__(self, key):
if isinstance(key, list):
return super().__getitem__(item_key) for item_keyin key
return super().__getitem__(key, list)
And you are good.
-
Or just use the existing operator.itemgetter:
from operator import itemgetter as ig
key_a, key_b = ig("key_a", "key_b")(mydict)