title: Create nested dictionary on the fly in Python
body: <p>Trying to understand how to create nested dictionaries on the fly. Ideally my dictionary would look something like:  </p>
  
  <pre><code>mydict = { 'Message 114861156': { 'email': ['user1@domain.com', 'user2@domain.com'] }, { 'status': 'Queued mail for delivery' }} 
  </code></pre>
  
  <p>Here's what i have so far: </p>
  
  <pre><code>sampledata = "Message 114861156 to user1@domain.com user2@domain.com  [InternalId=260927844] Queued mail for delivery'."
  
  makedict(sampledata)
  
  def makedict(results):
    newdict = {}
    for item in results:
      msgid = re.search(r'Message \d+', item)
      msgid = msgid.group()
      newdict[msgid]['emails'] = re.findall(r'\w+@\w+\.\w+', item)
      newdict[msgid]['status'] = re.findall(r'Queued mail for delivery', item)
  </code></pre>
  
  <p>has the following output:</p>
  
  <pre><code>Traceback (most recent call last):
    File "wildfires.py", line 57, in &lt;module&gt;
      striptheshit(q_result)
    File "wildfires.py", line 47, in striptheshit
      newdict[msgid]['emails'] = re.findall(r'\w+@\w+\.\w+', item)
  KeyError: 'Message 114861156'
  </code></pre>
  
  <p>How do you make a nested dictionary like this on the fly? </p>