title: Python - Create dictionary of dictionaries from CSV
body: <p>I have a CSV with the first column having many duplicate values, and the second column being a predetermined code that maps to a value in the third column, such as this:</p>
  
  <pre><code>1, a, 24
  1, b, 13
  1, c, 30
  1, d, 0
  2, a, 1
  2, b, 12
  2, c, 82
  2, d, 81
  3, a, 04
  3, b, 23
  3, c, 74
  3, d, 50
  </code></pre>
  
  <p>I'm trying to create a dictionary of dictionaries from a CSV, that would result in the following:</p>
  
  <pre><code>dict 1 = {'1':{'a':'24', 'b':'13', 'c':'30','d':'0'}, 
            '2':{'a':'1', 'b':'12', 'c':'82','d':'81'}, 
            ... }
  </code></pre>
  
  <p>My code creates the key values just fine, but the resulting value dictionaries are all empty (though some print statements have shown that they aren't during the run process)...</p>
  
  <pre><code>with open(file, mode='rb') as csvfile:
      reader = csv.reader(csvfile, delimiter=',')
  
      dict1 = {}  # creates main dict
      for row in reader:  # iterates through the rows of the csvfile
          if row[0] in dict1:
              dict2[row[1]] = row[2]  # adds another key, value to dict2
          else:
              dict1[row[0]] = {}  # creates a new key entry for the new      dict1 key
              dict2 = {}  # creates a new dict2 to start building as the value for the new dict1 key
              dict2[row[1]] = row[2]  # adds the first key, value pair for dict2
  </code></pre>