Lists and Tuples

The list data structure is central to most Python programs; Gurobi Python programs are no exception. We'll also rely heavily on a similar data structure, the tuple. Tuples are crucial to providing efficient and convenient access to Gurobi decision variables in Gurobi Python programs. The difference between a list and a tuple is subtle but important. We'll discuss it shortly.

Lists and tuples are both simply ordered collections of Python objects. A list is created and displayed as a comma-separated list of member objects, enclosed in square brackets. A tuple is similar, except that the member objects are enclosed in parenthesis. For example, [1, 2, 3] is a list, while (1, 2, 3) is a tuple. Similarly, ['Pens', 'Denver', 'New York'] is a list, while ('Pens', 'Denver', 'New York') is a tuple.

You can retrieve individual entries from a list or tuple using square brackets and zero-based indices:

gurobi> l = [1, 2.0, 'abc']
gurobi> t = (1, 2.0, 'abc')
gurobi> print l[0]
1
gurobi> print t[1]
2.0
gurobi> print l[2]
abc

What's the difference between a list and a tuple? A tuple is immutable, meaning that you can't modify it once it has been created. By contrast, you can add new members to a list, remove members, change existing members, etc. This immutable property allows you to use tuples as indices for dictionaries.

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.

Search