title: Python list of tuples to nested dict
body: <p>I have a data structure as follows:</p>
  
  <pre><code>[(a,x1,1),(a,x2,5),(b,x1,1) ...]
  </code></pre>
  
  <p>and i want to turn it into a nested dictionary of the form</p>
  
  <pre><code>{a:{x1:1, x2:5}, b:{x1:1}...}
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>dictdata = {}
  for row in rows:
      ean = row[1].encode('ascii','ignore')
      period = str(row[0])
      value = row[2]
      dictdata[ean]={} # init sub dictionary
      dictdata[ean][period] = value
  </code></pre>
  
  <p>but every time I do <code>dictdata[ean]={}</code>, the contents get erased, so this will not work. If I do not initialise the sub-dictionary, I also cannot get it to work.</p>
  
  <p>Any help appreciated</p>