Description
There are cases where keys of python dictionaries may need aliases. When two keys point to the same value, duplication can also be avoided with this feature.
How it is done currently:
foo = {
key1: "value",
key2: "value",
...
}
Here “value” is repeatedly used as value of the keys. When there are more keys with the same value, the more duplication occurs.
See the following syntax:
Conceptual syntax
foo = {
key1, key2, ...: "value"
}
Here, the duplication is less and the code looks cleaner. Do note that the punctuator ,
is just as per my concept. If it may hold conflicts with the item separators of dictionaries (which is also ,
), another punctuator can be used instead. E.g: |
, ;
, +
Use cases
- This feature can be extremely useful for libraries which tends to be user friendly.
- In big projects, when there is a confusion of key names (“was that
show
ordisplay
?”), this feature helps to avoid scrolling all the way up. - When key names are long, this feature helps to also accept acronyms or short names.
Examples
>>> foo = {
... "person1", "p1": "yes",
... "person2", "p2": "no",
... }
>>>
>>> a["person1"]
'yes'
>>> a["p1"]
'yes'