title: Python - Add to a dictionary using a string
body: <p>[Python 3.4.2]<br>
  I know this question sounds ridiculous, but I can't figure out where I'm messing up. I'm trying to add keys and values to a dictionary by using strings instead of quoted text. So instead of this,  </p>
  
  <pre><code>dict['key'] = value
  </code></pre>
  
  <p>this:</p>
  
  <pre><code>dict[key] = value
  </code></pre>
  
  <p>When I run the command above, I get this error:  </p>
  
  <pre><code>TypeError: 'str' object does not support item assignment  
  </code></pre>
  
  <p>I think Python is thinking that I'm trying to create a string, not add to a dictionary. I'm guessing I'm using the wrong syntax. This is what I'm trying to do:  </p>
  
  <pre><code>dict[string_for_key][string_for_value] = string_for_deeper_value  
  </code></pre>
  
  <p>I want this^ command to do this:</p>
  
  <pre><code>dict = {string_for_key: string_for_value: string_for_deeper_value}
  </code></pre>
  
  <p>I'm getting this error:  </p>
  
  <pre><code>TypeError: 'str' object does not support item assignment  
  </code></pre>
  
  <p>I should probably give some more context. I'm:  </p>
  
  <ol>
  <li>creating one dictionary  </li>
  <li>creating a copy of it <em>(because I need to edit the dictionary while iterating through it)</em>  </li>
  <li>iterating through the first dictionary while running some queries   </li>
  <li><em>trying</em> to assign a query's result as a value for each "key: value" in the dictionary. </li>
  </ol>
  
  <p>Here's a picture to show what I mean:  </p>
  
  <p>key: value: query_as_new_value</p>
  
  <p>-----EDIT-----  </p>
  
  <p>Sorry, I should have clarified: the dictionary's name is not actually 'dict'; I called it 'dict' in my question to show that it was a dictionary.</p>
  
  <p>-----EDIT-----</p>
  
  <p>I'll just post the whole process I'm writing in my script. The error occurs during the last command of the function. Commented out at the very bottom are some other things I've tried.   </p>
  
  <pre><code>from collections import defaultdict
  
  global query_line, pericope_p, pericope_f, pericope_e, pericope_g
  
  
  def _pre_query(self, typ):
      with open(self) as f:
          i = 1
          for line in f:
              if i == query_line:
                  break
              i += 1
          target = repr(line.strip())
      ###skipping some code
      pericope_dict_post[self][typ] = line.strip()
      #^Outputs error TypeError: 'str' object does not support item assignment
      return
  
  
  pericope_dict_pre = {'pericope-p.txt': 'pericope_p',
                       'pericope-f.txt': 'pericope_f',
                       'pericope-e.txt': 'pericope_e',
                       'pericope-g.txt': 'pericope_g'}
  pericope_dict_post = defaultdict(dict)
  #pericope_dict_post = defaultdict(list)
  #pericope_dict_post = {}
  for key, value in pericope_dict_pre.items():
      pericope_dict_post[key] = value
          #^Works
      #pericope_dict_post.update({key: value})
          #^Also works
      #pericope_dict_post.append(key)
          #^AttributeError: 'dict' object has no attribute 'append'
      #pericope_dict_post[key].append(value)
          #^AttributeError: 'dict' object has no attribute 'append'
      _pre_query(key, value)
  </code></pre>
  
  <p>-----FINAL EDIT-----  </p>
  
  <p><code>Matthias</code> helped me figure it out, although <code>acushner</code> had the solution too. I was trying to make the dictionary three "levels" deep, but Python dictionaries cannot work this way. Instead, I needed to create a nested dictionary. To use an illustration, I was trying to do <code>{key: value: value}</code> when I needed to do <code>{key: {key: value}}</code>.  </p>
  
  <p>To apply this to my code, I need to create the [second] dictionary with all three strings at once. So instead of this:</p>
  
  <pre><code>my_dict[key] = value1
  my_dict[key][value1] = value2
  </code></pre>
  
  <p>I need to do this:</p>
  
  <pre><code>my_dict[key][value1] = value2
  </code></pre>
  
  <p>Thanks a ton for all your help guys!</p>