I want to make a 2d dictionary with multiple keys per value. I do not want to make a tuple a key. But rather make many keys that will return the same value.
I know ho
Does this help at all?
class MultiDict(dict):
# define __setitem__ to set multiple keys if the key is iterable
def __setitem__(self, key, value):
try:
# attempt to iterate though items in the key
for val in key:
dict.__setitem__(self, val, value)
except:
# not iterable (or some other error, but just a demo)
# just set that key
dict.__setitem__(self, key, value)
x = MultiDict()
x["a"]=10
x["b","c"] = 20
print x
The output is
{'a': 10, 'c': 20, 'b': 20}