Tuples are immutable lists.
In order to define a tuple, use this syntax:
binding_partners = ( "BIOGRID:112315", "BIOGRID:108607" # no comma )
Leaving an additional comma after the last element also works:
binding_partners = ( "BIOGRID:112315", "BIOGRID:108607", # this comma makes no difference )
Tuples should be used instead of lists whenever mutability is an issue, for instance as dictionary keys.
Example. Let’s create a bunch of different tuples:
# A tuple of integers integers = (1, 2, 3, 1) # A tuple of strings uniprot_proteins = ("Y08501", "Q95747") # A tuple of heterogeneous objects things = ("Y08501", 0.13, "Q95747", 0.96) # A tuple of tuples two_level_tuple = ( ("Y08501", 120, 520), ("Q95747", 550, 920), )
Example. Empty and length-one tuples are a bit weird. I will not explain how to create empty tuples, they are very rare (and not really useful).
But let’s assume for the sake of illustration that we need to create
a tuple with just one element, say the integer
0. This does not work:
tuple = (0) print tuple print type(tuple) # not a tuple!
The reason is that
(0) is interpreted by Python as an arithmetical
expression. Indeed, this is what the syntax means: an integer within a pair of
In order to define such a one-element tuple, write:
tuple = (0,) print tuple print type(tuple) # it's a tuple!
Now the ambiguity is gone: the comma
, in there makes it clear to Python
that we want to create a tuple – except that it contains only one element.
Most list operators can be used with tuples as well. Of course, since tuples are immutable, operators that change the value of lists (like the assignment operator) do not carry over to tuples.
||Compute the length of a tuple|
||Concatenate two tuples|
||Replicate a tuple|
||Check whether an element appears in a tuple|
||Extract a sub-tuple|
Example. Let’s create a tuple with mixed object types:
protein_info = ("2B0Q", "phosphotransferase", "PF03881")
It holds information about a given protein (PDB identifier 2B0Q). We can access the various elements as if it were a list:
protein_id = protein_info print protein_id protein_name = protein_info print protein_name
However, we can not modify the tuple in any way:
>>> protein_info = "1A3A" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
||Return the index of the first occurrence of an object|
||Count the number of occurrences of an object|
These are identical to their list counterparts.
In some cases you want to modify a tuple, but you can’t. In other cases you want to use a list as a key in a dictionary, but you can’t.
The tricks is to convert between tuples and lists as needed. In order to
convert between the two, use the
l = range(10) print l, type(l) t = tuple(l) print t, type(t) l = list(t) print l, type(l)
Example. We want to modify a given tuple:
t = ("I", "am", "a", "tuple") print t
Let’s proceed as follows:
l = list(t) l.insert(2, "pretty") t2 = tuple(l) print t print t2
Of course, we did not modify the original tuple; rather, we created a new tuple from an intermediate list.