Try our new documentation site (beta).


Gurobi tuple dict. This is a sub-class of the Python dict class that is designed to efficiently support a usage pattern that is quite common when building optimization models. In particular, a tupledict is a Python dict where the keys represent variable indices, and the values are typically Gurobi Var objects. Objects of this class make it easier to build linear expressions on sets of Gurobi variables, using syntax and semantics.

You typically build a tupledict by calling Model.addVars. Once you've created a tupledict d, you can use d.sum() to create a linear expression that captures the sum of the variables in the tupledict. You can also use a command like d.sum(1, '*', 5) to create a sum over a subset of the variables in d. Assuming the keys for the tupledict are tuples containing three fields, this statement would create a linear expression that captures the sum over all variables in d whose keys contain a 1 in the first field of the tuple and a 5 in the third field (the '*' character is a wildcard that indicates that any value is acceptable in that field). You can also use to create a linear expression where the coefficients are pulled from the dictionary coeff. For example, if d(1,2,5) contains variable x and coeff(1,2,5) is 2.0, then the resulting expression would include term <span>$</span>2.0*x<span>$</span>.

To access the members of a tupledict, you can use standard dict indexing. For example, d[1,2] returns the value associated with tuple (1,2).

Note that a tupledict key must be a tuple of scalar values (int, float, string, ...). Thus, you can use (1, 2.0, 'abc') as a key, but you can't use ((1, 2.0), 'abc').

Note that tupledict objects build and maintain a set of internal data structures to support efficient select operations. If you wish to reclaim the storage associated with these data structures, you can call the clean function.


Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.