This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Sequence type - Tuples

Python also provides us with another sequence type called tuples.

First question: How do you pronounce tuple?

The Python Benevolent dictator for life himself has answered the question:

How do you pronounce tuple? Guido von Rossum: 'I pronounce tuple too-pull on Mon/Wed/Fri and tub-pull on Tue/Thu/Sat. On Sunday I don't talk about them. :)'

Tuples are like lists. The main difference is that lists are mutable, whereas tuples are immutable.

“Mutable” comes from the word mutate. Think mutants.

Teenage Mutant Ninja Turtles

Image credits

So mutable means “can be modified”, while immutable means “cannot be changed”.

So you can add/remove items to/from a list after you have created the list (for example by using the append() method), but you cannot add or remove elements from a tuple once you have created it.

Tuples are useful for representing ordered, fixed-size grouping of elements, like 2D coordinates \((x, y)\) or 3D coordinates \((x, y, z)\). Here, you should treat each point as a fixed vector in the coordinate space, not something that can be modified.

For example, \((5, 3)\) is an entity in its own right, so you cannot replace, remove, or add new members to it. If you want a new vector, e.g. \((2, 3)\), you should create a brand new vector rather than modifying the \(5\) in \((5, 3)\) to be \(2\).

Sort of like how you cannot replace Edward in the group Jedward with someone else and still call them Jedward the twins. You have to form a new group with John and the new member (and they will not be twins!).

Jedward

Image credits: Michael Dorausch from Venice, USA / CC BY-SA

Creating a new tuple

Creating a new tuple in Python is just like creating a list, except that you use (parenthesis) instead of [square brackets].

>>> my_vector = (1, 3, 4)
>>> print(my_vector)

You can also cast an existing list (or any Python sequence type) into a new tuple object.

>>> my_list = [1, 3, 4]
>>> my_vector = tuple(my_list)
>>> print(my_vector)

If you want an empty tuple:

>>> empty_tuple = ()
>>> print(empty_tuple)
>>> empty_tuple2 = tuple()
>>> print(empty_tuple2)

A Python quirk! If your tuple has one single element, you should include a comma after the first elementi to indicate that it is a tuple. Otherwise, Python will treat it as a single variable (the parenthesis will just be a parenthesis). Ugly, I know.

>>> non_tuple = (1)
>>> print(non_tuple)
>>> print(type(non_tuple))
>>> singleton = (1, )
>>> print(singleton)
>>> print(type(singleton))

Accessing elements in a tuple

You can access elements in a tuple just like in a list:

>>> x = (1,2,3,4,5)
>>> print(x[0])
>>> print(x[1:3])
>>> print(x[-1])
>>> print(x[:4:2])
>>> print(x[::-1])

… but you cannot modify the elements. Try the following, and understand the error messages produced by Python.

>>> x[1] = 6      # Python will complain!
>>> x.append(6)   # ... and complain again!

If you need a new tuple, then create a new tuple! (But perhaps you should have just used a list instead?)

>>> x = (1, 2, 3)
>>> print(x)
>>> x = (1, 2, 3, 4)
>>> print(x)

… although you might actually be able to change the elements inside a tuple if they are mutable. But again, think about whether you should really be using a tuple if this is happening!

>>> x = [1, 2]
>>> y = (1, x, 3, 4)
>>> print(y)
>>> x.append(3)
>>> print(x)
>>> print(y)

Operators

You can use the +, *, and in operators in the same way as lists.

>>> print((1, 2) + (3, 4))
>>> print((1, 2) * 3)
>>> print(1 in (1,2))
>>> print(1 not in (1,2))

Just one more thing…

Python has another special sequence type called range(), but we will discuss this later when we talk about loops.