Dictionary of Dicts question

William Purcell williamhpurcell at gmail.com
Thu Oct 16 16:55:19 EDT 2008


I believe that
myDict['TestName'] = {'NewFileName': {}, }
should be
myDict['TestName']['NewFileName'] = {}

-Bill

On Thu, Oct 16, 2008 at 3:44 PM, Chris Rebert <clp at rebertia.com> wrote:

> On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen at adobe.com>
> wrote:
> > I'm working with a Dictionary of Dicts. Something like this:
> >
> > myDict = {
> >                                                 'TestName': {
> >
> 'FileName':{
> >
> > 'ct_init':1234,
> >
> > 'psl_init':5678,
> >
> > 'total_test_time':7890,
> >
> > 'psl_shutdown':8765,
> >
> > 'ct_shutdown':9021,
> >
> > 'total_time':3421,
> >                                                                 },
> >                                                 }
> > }
> >
> > Accessing values is pretty straightforward (nice change from my Perl
> days).
> > For example:
> >
> > myDict['TestName']['FileName']['ct_shutdown']
> >
> > in Python interpreter yields
> >
> > 9021
> >
> > However, when I try to add, let's say, a new FileName entry, I end up
> > replacing the previous FileName entry.
> >
> > In Python interpreter, I try:
> >
> > myDict['TestName'] = {'NewFileName': {}, }
> >
> > I get
> >
> > {'TestName': {'NewFileName': {}}}
>
> Right, this clobbers the existing entry with this new blank one. This
> is evidenced by the fact that you're performing an _assignment_ on a
> dictionary key rather than calling a _mutator_ method on a dictionary
> value. A dictionary has only one value for a given key (but
> importantly, that value can be a list).
>
> >
> > So, how do I add a new entry without replacing the old entry?
>
> Switch to a Dict of Lists of Dicts and append to the appropriate list
> when adding the new entry, or preferably, start using objects instead
> of ad-hoc nested dictionaries.
>
> Regards,
> Chris
> --
> Follow the path of the Iguana...
> http://rebertia.com
>
> >
> > Thanks
> >
> > John Townsend (5-7204),
> > AGM-FL and PSL QE Lead
> >
> >
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081016/03a44614/attachment-0001.html>


More information about the Python-list mailing list