title: Python: How to populate nested dict with attributes and values from parsed XML string?
body: <p>I have a dict containing identifiers as keys, with an XML string as their respective values. I want to parse the attributes and values from the XML and automagically populate a dict with them, under their respective identifier keys.</p>
  <pre><code>import xml.etree.ElementTree as etree
  
  employees = {
      'employee_0': '&lt;Person&gt;&lt;Attribute name=&quot;name&quot;&gt;&lt;Value&gt;Bill Johnson&lt;/Value&gt;&lt;/Attribute&gt;&lt;Attribute name=&quot;city&quot;&gt;&lt;Value&gt;New York&lt;/Value&gt;&lt;/Attribute&gt;&lt;Attribute name=&quot;email&quot;&gt;&lt;Value&gt;bill.johnson@email.com&lt;/Value&gt;&lt;/Attribute&gt;&lt;/Person&gt;', 
      'employee_1': '&lt;Person&gt;&lt;Attribute name=&quot;name&quot;&gt;&lt;Value&gt;Amanda Philips&lt;/Value&gt;&lt;/Attribute&gt;&lt;Attribute name=&quot;city&quot;&gt;&lt;Value&gt;Los Angeles&lt;/Value&gt;&lt;/Attribute&gt;&lt;Attribute name=&quot;email&quot;&gt;&lt;Value&gt;amanda.philips@email.com&lt;/Value&gt;&lt;/Attribute&gt;&lt;/Person&gt;'
  }
  
  for identifier_key in employees:
      xml = etree.fromstring(employees[identifier_key])
      for key in xml:
          key_str = key.attrib[&quot;name&quot;]
          for value in key:
              value_str = value.text
              employees[identifier_key][key_str] = value_str
  </code></pre>
  <p>I want the <code>employees</code> dict to result in this:</p>
  <pre><code>{
      &quot;employee_0&quot;: {
          &quot;name&quot;: &quot;Bill Johnson&quot;,
          &quot;city&quot;: &quot;New York&quot;,
          &quot;email&quot;: &quot;bill.johnson@email.com&quot;
          },
      &quot;employee_1&quot;: {
          &quot;name&quot;: &quot;Amanda Philips&quot;,
          &quot;city&quot;: &quot;Los Angeles&quot;,
          &quot;email&quot;: &quot;amanda.philips@email.com&quot;
          }
  }
  </code></pre>
  <p>But in the code above, we get a <code>TypeError: 'str' object does not support item assignment</code>. My questions are:</p>
  <ul>
  <li>Why do we get this error? It seems like this should be the proper way to populate the dict. If I instead use <code>employees[identifier_key] = { key_str: value_str }</code> it will overwrite the previous iteration. I have tried <code>.update()</code> too, without luck. How can this operation be accomplished?</li>
  <li>How can the operation be accomplished in a nice and clean way, e.g. using dict comprehension? I'm having difficulty putting together the syntax for it.</li>
  </ul>