469,609 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Re: Dictionary of Dicts question

On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jt******@adobe.comwrote:
I'm working with a Dictionary of Dicts. Something like this:

myDict = {
'TestName': {







Accessing values is pretty straightforward (nice change from my Perl days).
For example:


in Python interpreter yields


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.

Follow the path of the Iguana...

John Townsend (5-7204),
AGM-FL and PSL QE Lead


Oct 16 '08 #1
0 820

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

125 posts views Thread by Raymond Hettinger | last post: by
14 posts views Thread by Antoon Pardon | last post: by
5 posts views Thread by Jon Smirl | last post: by
18 posts views Thread by Marko.Cain.23 | last post: by
5 posts views Thread by Greg Corradini | last post: by
4 posts views Thread by John Townsend | last post: by
14 posts views Thread by Prateek | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.