Expandable 2D Dictionaries?

Robert Dailey rcdailey at gmail.com
Fri Jul 6 12:22:35 EDT 2007


On Jul 6, 11:09 am, Jean-Paul Calderone <exar... at divmod.com> wrote:
> On Fri, 06 Jul 2007 15:43:55 -0000, Robert Dailey <rcdai... at gmail.com> 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?
>
> This gets much easier if you change your structure around a bit:
>
>     d = {}
>     d["cat", "paw"] = "some string"
>
> Jean-Paul

I like this format. I'm not familiar with it however. In my research
of python I was not aware it was legal to have comma operators inside
of the brackets. What does this mean? Is there some terminology that I
can search for to research this concept? Thanks.




More information about the Python-list mailing list