Skip to content

Tuples

Conceptually, tuples are similar to lists, but with one important difference: tuples are unchangeable. Once they have been created, they can no longer be changed, only a new tuple can be created. The notation is very similar to lists, but round brackets are used instead of square brackets:

elements = ("H", "He", "Li")

The special feature here is that a list with only one element must be labelled with an additional comma, because otherwise it is not possible to distinguish it from an expression in brackets:

elements = ("H",)

An empty tuple, on the other hand, is unambiguous (), as there is no expression in the brackets, so there is no confusion.

With the exception of methods that change the contents of a list, all methods and operations can also be applied to tuples:

elements = ("H", "He", "Li")
elements[-1] # Results in "Li"
len(elements) # Results in 3

Some operations create a new tuple, e.g. merging tuples:

elements = ("H", "He", "Li")
elements + ("Be", "B", "C") # Results in ("H", "He", "Li", "Be", "B", "C")