Custom dict to prevent keys from being overridden

Julien jphalip at gmail.com
Sat Aug 27 21:59:10 EDT 2011


Hi,

With a simple dict, the following happens:

>>> d = {
...   'a': 1,
...   'b': 2,
...   'a': 3
... }
>>> d
{'a': 3, 'b': 2}

... i.e. the value for the 'a' key gets overridden.

What I'd like to achieve is:

>>> d = {
...   'a': 1,
...   'b': 2,
...   'a': 3
... }
Error: The key 'a' already exists.

Is that possible, and if so, how?

Many thanks!

Kind regards,

Julien



More information about the Python-list mailing list