title: Convert Numpy array to Dictionary using Multidict
body: <p>I have a piece of code that generates a matrix in a Numpy array format where the first column contains a variable to indicate the index and the other columns contain other variables.</p>
  <p>I would like to convert this to a dictionary where every row of the matrix becomes an entry with the first column value (the index) as key and the other column variables as values with the same key.</p>
  <p>So something like:</p>
  <pre><code>matrix = np.array([ [1, a,b,c], [2, d,e,f], [3, g,h,i] ])
  </code></pre>
  <p>should become a dictionary formatted at:</p>
  <pre><code>{1: a, b, c
   2: d, e, f
   3: g, h, i}
  </code></pre>
  <p>where each <code>a, d, g</code> have the same key, <code>b, e, h</code> have the same key etc.</p>
  <p>Using <a href="https://www.gurobi.com/documentation/9.0/refman/py_multidict.html" rel="nofollow noreferrer">multidict</a> (as I am using the code for an optimization model in Gurobi), I have been able to do this for one row using:</p>
  <pre><code>key1,value1,value2,value3 = gurobipy.multidict({tuple(matrix[0,0]):tuple(matrix[0,1:]))})
  </code></pre>
  <p>However I cannot get it to work for all rows of the matrix. If I change the index for the row from 0 to all:</p>
  <pre><code>key,value1,value2,value3 = gurobipy.multidict({tuple(matrix[:,0]):tuple(matrix[:,1:]))})
  </code></pre>
  <p>I get an error 'not enough values to unpack'. It seems like the second argument for the values, <code>tuple(matrix[:,1:])</code>, does not (properly) parse all column entries per row from <code>1:end</code>.</p>
  <p>If I specify the column index for each value, it does work:</p>
  <pre><code>key1,value1,value2,value3 = gurobipy.multidict({tuple(matrix[:,0]):[tuple(matrix[:,1]),tuple(matrix[:,2]),tuple(matrix[:,3])]})
  </code></pre>
  <p>However this is not robust for me as the amount of columns may change. So instead, I'd like the code for each and every row to take the first column index value as the main, unique key and all subsequent column variables as values with the same key for each column.</p>
  <p>Is there a way to rewrite my broken code to achieve this?</p>