On Jul 6, 10:54 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > Robert Dailey wrote: > > Hi, > > > I am interested in creating an expandable (dynamic) 2D dictionary. For > > example: > > > myvar["cat"]["paw"] = "Some String" > > > The above example assumes "myvar" is declared. In order for this to > > work, I have to know ahead of time the contents of the dictionary. For > > the above to work, my declaration must look like: > > > myvar = {"cat": {"paw":""} } > > > I would like to not have to declare my dictionary like this, as it > > does not allow it to be expandable. I'm very new to Python (I'm a > > professional C++ programmer. Any comparisons to C++ would help me > > understand concepts). > > > Is there a way that when I index into my dictionary using an "unknown" > > index (string), that python will dynamically add that key/value pair? > > Not really, unless you know that the first level of values are _always_ > dicts. > > What you can do is to use > > myvar.setdefault('cat', {})['paw'] = "Some String" > > Diez
Could you emphasize what you mean by "unless you know that the first level of values are _always_ dicts."? Could I create a class wrapper that automates this? I could have an insert() method of some sort. However I would have to research if it's possible to overload operators in python. -- http://mail.python.org/mailman/listinfo/python-list