Expandable 2D Dictionaries?

Steve Holden steve at holdenweb.com
Fri Jul 6 19:31:21 EDT 2007


Jean-Paul Calderone wrote:
> On Fri, 06 Jul 2007 15:43:55 -0000, Robert Dailey <rcdailey 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

Unfortunately that makes it somewhat more difficult to retrieve the set 
of entries whose first index is "cat". I realise that we don't have much 
in the way of use cases here, but it would seem that the original 
motivation for a two-level dictionary might have been such a requirement.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list