Multidimensional Dictionaries: Possible?
Stewart Wilson
wilson at nortelnetworks.com
Tue Jan 25 16:44:42 EST 2000
Skip Montanaro wrote:
>
> If what you really want are nested dicts, then you should use something like
>
> >>> dict = {}
> >>> dict[1] = {}
> >>> dict[1][2] = 'foo'
> >>> dict.keys()
> [1]
> >>> dict[1].keys()
> [2]
>
In addition, and to answer your original question on incrementing the
value, if your top level dictionary contains the first key, then you can
do:
dict["key1"]["key2"] = dict["key1"].get("key2",0) + 1
As in this example:
>>> dict = {}
>>> dict["key1"] = {}
>>> dict["key1"]["key2"] = dict["key1"].get("key2",0) + 1
>>> dict["key1"]["key2"]
1
>>> dict["key1"]["key2"] = dict["key1"].get("key2",0) + 1
>>> dict["key1"]["key2"]
2
---
Stewart Wilson
wilson at nortelnetworks.com
More information about the Python-list
mailing list