title: KeyError when trying to use dictionary generated from SQLite in Python
body: <p>This is a follow up question to one I asked a couple of days ago. At that point, I encountered the following error, when trying to use SQLITE to generate a dictionary in Python:</p>
  
  <pre><code>'Traceback (most recent call last):
     File "transportation problem.py", line 20, in &lt;module&gt;
     I = set([i for (i,k) in d])
  ValueError: too many values to unpack
  </code></pre>
  
  <p>This could be solved, thanks to the suggestions made by Andy Hayden worked fine at first, but when I implemented it into my code, I started getting the following error:</p>
  
  <pre><code>c[i,j,k] = cost[i,j] * weight[k]
  KeyError: ((1, 2), 1)
  </code></pre>
  
  <p>I am not sure, what caused this error, but would be very glad if somebody could point me in the right direction, of how I would be able to get this code to work. My code looks like follows:</p>
  
  <pre><code>#DISTANCE    
  cur.execute('SELECT idcustomer,idproduct,demand FROM demand')
  result = cur.fetchall()
  for idcustomer,idproduct,demand in result:
        d[idcustomer,idproduct] = demand
        I = set([i for (i,k) in d.iteritems()])
        K = set([k for (i,k) in d.iteritems()])
  #CAPACITY
  
  cur.execute('select idfactory,capacity from factory')
  result = cur.fetchall()
  J, M = multidict(result)
  
  produce = {1:[2,4], 2:[1,2,3], 3:[2,3,4]} 
  
  #WEIGHT
  cur.execute('SELECT idproduct,weight FROM product')
  result = cur.fetchall()
  K, weight = multidict(result)          
  
  #COST
  cur.execute('SELECT idcustomer,idfactory,distance FROM distance')
  result = cur.fetchall()
  for idcustomer,idfactory,distance in result:
      cost[idcustomer,idfactory] = distance
  
  c = {}
  for i in I:
      for j in J:
          for k in produce[j]:
              c[i,j,k] = cost[i,j] * weight[k]
  </code></pre>
  
  <p>The error is produced in the last line of this code. When I input the SQLITE data manually, and revert 'd.iteritems' back to 'd', the code works without a problem. </p>
  
  <pre><code> produce = {1:[2,4], 2:[1,2,3], 3:[2,3,4]}  
   K, weight = multidict({1:5, 2:2, 3:3, 4:4})            
        cost = {(1,1):4,  (1,2):6, (1,3):9,        
                (2,1):5,  (2,2):4, (2,3):7,
                (3,1):6,  (3,2):3, (3,3):4,
                (4,1):8,  (4,2):5, (4,3):3,
                (5,1):10, (5,2):8, (5,3):4,
                }
        c = {}
        for i in I:
          for j in J:
             for k in produce[j]:
                 c[i,j,k] = cost[i,j] * weight[k]
  </code></pre>
  
  <p>Any suggestions would again be more than welcome!</p>
  
  <p>Edit: </p>
  
  <p>Following a suggestion of @abarnert, I have added the values of I, J, K, weight and cost.</p>
  
  <pre><code>{'I': set([(1, 2), (5, 4), (1, 3), (3, 3), (5, 2), (4, 2), (3, 1), (3, 2), (2, 1), (1, 1), (2, 3), (1, 4), (4, 3), (2, 2), (3, 4), (5, 1), (4, 1), (2, 4), (4, 4), (5, 3)])}
  
  {'J': [1, 2, 3]}
  
  {'K': [1, 2, 3]}
  {'weight': {1: 5.0, 2: 2.0, 3: 3.0, 4: 4.0}}
  {'cost': {(1, 2): 6.0, (3, 2): 3.0, (1, 3): 9.0, (3, 3): 4.0, (5, 2): 8.0, (3, 1): 6.0, (2, 1): 5.0, (2, 3): 7.0, (4, 3): 3.0, (2, 2): 4.0, (5, 1): 10.0, (4, 2): 5.0, (4, 1): 8.0, (1, 1): 4.0, (5, 3): 4.0}}
  </code></pre>