Python: deep appending to dictionary? - in a single expression
<p>How do I, in a <strong>single expression</strong>, get a dictionary
where one key-value pair has been added to a sub-dictionary 
in some input dictionary? The input dictionary should be left unchanged. It 
can be assumed the sub-dictionary does exist and that the new key-value pair is not already in the sub-dictionary.</p>

<p><strong>Update 2</strong> (see below for definition of "SOsurvivalConditions", etc.):</p>

<p>The most concise way is:</p>

<pre><code>(SOsurvivalConditions['firstCondition'].setdefault('synonym', 'A modern form of RTFM is: Google It.'), SOsurvivalConditions)[-1]
</code></pre>

<p><strong>Update 1</strong> :</p>

<p>This meets the given requirements and does not have the
side-effect of modifying the input dictionary:</p>

<pre><code>dict((k,dict(v, synonym='A modern form of RTFM is: Google It.') if k == "firstCondition" else v) for k,v in SOsurvivalConditions.iteritems())
</code></pre>

<p>However the more concise (but statement only) way can
be adapted with a helper function, e.g.:</p>

<pre><code>import copy
def dictDeepAdd(inputDict, dictKey, newKey, newValue):
    """
      Adds new key-value pair to a sub-dictionary and
      returns a new version of inputDict.

      dictKey is the key in inputDict for which a new
      key-value pair is added.

      Side-effect: none (does not change inputDict).
    """
    toReturn = copy.deepcopy(inputDict)
    toReturn[dictKey][newKey] = newValue
    return toReturn

dictDeepAdd(
                 SOsurvivalConditions,
                 'firstCondition',
                 'synonym',
                 'A modern form of RTFM is: Google It.'
           )
</code></pre>

<p><hr /></p>

<p>The Example:</p>

<pre><code>goodStyle = \
{
    'answer': 'RTFM responses are not acceptable on StackOverflow - Joel Spolsky has repeatly said so in the StackOverflow podcasts.',
    'RTFM'  : 'RTFM is, in the less offensive version, an abbrevation for Read The Fine Manual.',
}

SOsurvivalConditions = \
{
    'moodImperative' : 'be happy',
    'firstCondition' : goodStyle,
}
</code></pre>

<p>'firstCondition' in SOsurvivalConditions now has 2 key-value pairs.
A new key-value pair, ('synonym', 'A modern form of RTFM is: Google It.'),
needs to be appended and the result should be available in a single expression.</p>

<p>This works (one line, but broken into several here):</p>

<pre><code>{
    'moodImperative': SOsurvivalConditions['moodImperative'],
    'firstCondition' :
        dict(
               SOsurvivalConditions['firstCondition'],
               synonym = 'A modern form of RTFM is: Google It.'
            )
}
</code></pre>

<p>and returns:</p>

<p>{'moodImperative': 'be happy', 'firstCondition': {'answer': 'RTFM responses are not acceptable on StackOverflow - Joel Spolsky has repeatly said so in the StackOverflow podcasts.', 'RTFM': 'RTFM is, in the less offensive version, an abbrevation for Read The Fine Manual.', 'synonym': 'A modern form of RTFM is: Google It.'}}</p>

<p>However there is a lot of redundancy in this
expression - all keys are repeated. And 'firstCondition' appears two times. Is there a more
elegant way?</p>

<p>(The names and the content of the datastructures here are made up, but represent a real problem I encountered today. Python version: 2.6.2.).</p>
