I want to add elements (key-value-pairs) to a dict
. But I want to prevent overwriting existing values when the key exists.
But I don't want to do an if
check. I would prefer an exception.
d = {}
d['a'] = 1
d['b'] = 2
d['a'] = 3 # <-- raise Exception! "KeyExistsException'
What I don't want
if not 'a' in d:
d['a'] = 3