title: python sqlite3 sum each type
body: <p>I have a table (foos) which is a list of Foos, one row per type of Foo.  A second table (items) is a list in which each row is the type of Foo and an amount (and other information).  For example, Foo3, 45.2 and Foo2, 12.34.</p>
  
  <p>I'd like to determine the total of the amounts for each type of Foo.</p>
  
  <p>This is my existing code, but there must be a better (more standard or efficient) way:</p>
  
  <pre><code>cursor.execute('''select type from foos''')
  foo_types = cursor.fetchall()
  results = []
  for ft in foo_types:
      cursor.execute('''select sum(amount) from items
          where foo_type =?''', ft)
      results.append((ft, cursor.fetchone()))
  </code></pre>
  
  <p>How should I code this?</p>