Try our new documentation site (beta).
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
.