title: list comprehension to build a nested dictionary from a list of tuples
body: <p>I have data (counts) indexed by <code>user_id</code> and <code>analysis_type_id</code> obtained from a database. It's a list of 3-tuple. Sample data:</p>
  
  <pre><code>counts  = [(4, 1, 4), (3, 5, 4), (2, 10, 4), (2, 10, 5)]
  </code></pre>
  
  <p>where the first item of each tuple is the <code>count</code>, the second the <code>analysis_type_id</code>, and the last the <code>user_id</code>.</p>
  
  <p>I'd like to place that into a dictionary, so i can retrieve the counts quickly: given a <code>user_id</code> and <code>analysis_type_id</code>. It would have to be a two-level dictionary. Is there any better structure?</p>
  
  <p>To construct the two-level dictionary "by hand", I would code:</p>
  
  <pre><code>dict = {4:{1:4,5:3,10:2},5:{10:2}}
  </code></pre>
  
  <p>Where <code>user_id</code> is the first dict key level, <code>analysis_type_id</code> is the second (sub-) key, and the <code>count</code> is the value inside the dict.</p>
  
  <p>How would I create the "double-depth" in dict keys through list comprehension? 
  Or do I need to resort to a nested for-loop, where I first iterate through unique <code>user_id</code> values, then find matching <code>analysis_type_id</code> and fill in the counts ... one-at-a-time into the dict?</p>