Of course, I forgot to mention that you can use tuples as dictionary keys.<br><br>A common idiom is:<br><br>pdb[(dataset, modulename, parametername, &#39;value&#39;)] = value.<br><br>And if that works for you, it&#39;s much simpler. But you lose the ability to use, say, pdb[dataset][modulename] as a dictionary on its own.
<br><br>Remco<br><br><br><div class="gmail_quote">On Jan 24, 2008 8:20 AM, Garry Willgoose &lt;<a href="mailto:garry.willgoose@newcastle.edu.au">garry.willgoose@newcastle.edu.au</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Is there any easy way to create a nested dictionary. I want to be<br>able to allocate like<br><br>pdb[dataset][modulename][parametername][&#39;value&#39;]=value<br><br>where dataset, modulename, parametername are variables that are
<br>determined within loops nested 3 deep, and value comes from a<br>database call. Prior to the nested loops I do not know what the<br>values of dataset, modulename, parametername will range over, but I<br>do know pdb needs to be nested 3 deep. What I&#39;d like to do is
<br>something like this<br><br>pdb={}<br>for dataset in dataset_list:<br> &nbsp; &nbsp; &nbsp; &nbsp;modulename_list=getmodules(dataset)<br> &nbsp; &nbsp; &nbsp; &nbsp;for modulename in modulename_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parametername_list=getparameters(dataset,modulename)
<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for parametername in parametername_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value=getvalue(dataset, modulename, parametername)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pdb[dataset][modulename][parametername][&#39;value&#39;]=value
<br><br>What I&#39;m currently doing is<br><br>pdb={}<br>for dataset in dataset_list:<br> &nbsp; &nbsp; &nbsp; &nbsp;modulename_list=getmodules(dataset)<br> &nbsp; &nbsp; &nbsp; &nbsp;moduledict={}<br> &nbsp; &nbsp; &nbsp; &nbsp;for modulename in modulename_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parametername_list=getparameters(dataset,modulename)
<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valuedict={}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for parametername in parametername_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value=getvalue(dataset, modulename, parametername)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valuedict[&#39;value&#39;]=value
<br># &nbsp;valuedict needs to be a dictionary because there is other stuff<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valuedict[&#39;otherstuff]=otherstuff<br>...<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parameterdict[parametername]=valuedict.copy()<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;moduledict[modeulename]=
copy.deepcopy(parameterdict)<br> &nbsp; &nbsp; &nbsp; &nbsp;pdb[dataset]=copy.deepcopy(moduledict)<br><br><br>Now I know the 2nd is not that much more complex but this is a pretty<br>common construct in what I&#39;m doing so I&#39;m just wondering if there is
<br>a clear and simple shortcut ;-)<br><br><br>====================================================================<br>Prof Garry Willgoose,<br>Australian Professorial Fellow in Environmental Engineering,<br>Director, Centre for Climate Impact Management (C2IM),
<br>School of Engineering, The University of Newcastle,<br>Callaghan, 2308<br>Australia.<br><br>Centre webpage: <a href="http://www.c2im.org.au" target="_blank">www.c2im.org.au</a><br><br>Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574
<br>(Fri PM-Mon)<br>FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal<br>and Telluric)<br>Env. Engg. Secretary: (International) +61 2 4921 6042<br><br>email: &nbsp;<a href="mailto:garry.willgoose@newcastle.edu.au">
garry.willgoose@newcastle.edu.au</a>;<br><a href="mailto:g.willgoose@telluricresearch.com">g.willgoose@telluricresearch.com</a><br>email-for-life: <a href="mailto:garry.willgoose@alum.mit.edu">garry.willgoose@alum.mit.edu
</a><br>personal webpage: <a href="http://www.telluricresearch.com/garry" target="_blank">www.telluricresearch.com/garry</a><br>====================================================================<br>&quot;Do not go where the path may lead, go instead where there is no path
<br>and leave a trail&quot;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ralph Waldo Emerson<br>====================================================================<br><br><br><br><br><br>_______________________________________________<br>
Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div>
<br>