Skip to content

Tuples

~15 min ยท prerequisite: Lists

What you will be able to do

  • Create a tuple and read its items by index, the same way you read a list.
  • Explain why a tuple cannot be changed, and choose between a list and a tuple.
  • Use unpacking to give several names to the items of a tuple in one line, including the swap a, b = b, a.

The idea

A tuple is an ordered collection of values, like a list, but it is immutable. Once you create a tuple, you cannot add, remove, or replace an item. A tuple is written with parentheses, such as (3, 4), and its items are separated by commas. Parentheses are optional in many places, so point = 3, 4 also creates a tuple. Read the tuple by index exactly as you read a list: point[0] is 3.

The commas make the tuple, not the parentheses. A single item needs a trailing comma. The expression (5) is just the integer 5, and (5,) is a tuple with one item. Writing type((5,)) shows <class 'tuple'>.

Tuples are used when a group of values belongs together and should not change. For example, a pair (x, y) describes one point, and a row ("Ada", 36) describes one person. A list suits a collection that grows or shrinks, such as a shopping list. The choice also tells a reader something. A tuple says "these values form one fixed record".

The most useful tuple feature is unpacking. When you write x, y = point, Python takes the first item and stores it in x, and the second item goes to y. The number of names on the left must match the number of items on the right. Unpacking also gives you the swap. The line a, b = b, a first builds the tuple (b, a) from the old values, then unpacks it into the two names. No temporary variable is needed.

Tuples support len, in, for, and the methods count and index. The method count(x) returns how many items equal x, and index(x) returns the position of the first one.

Worked example

point = (3, 4)
print(point[0])
print(len(point))

x, y = point
print("x is", x, "and y is", y)

a = 10
b = 20
a, b = b, a
print(a, b)

single = (5,)
print(type(single))
print(type((5)))

Line by line, this is what Python does:

  1. point holds the tuple (3, 4). point[0] reads the first item, which is 3.
  2. len(point) counts two items.
  3. x, y = point unpacks the tuple. Now x is 3 and y is 4.
  4. a, b = b, a builds the tuple (20, 10) from the old values and unpacks it, so a becomes 20 and b becomes 10.
  5. (5,) is a tuple with one item, so type reports tuple. The expression (5) has no comma, so the parentheses are only grouping, and type reports int.

The output is:

3
2
x is 3 and y is 4
20 10
<class 'tuple'>
<class 'int'>

Try it

The first block checks reading and unpacking. The second block checks the trailing comma and the swap. The third block checks the methods count and index.

point = (3, 4)
assert point[0] == 3
assert point[-1] == 4
assert len(point) == 2
x, y = point
assert x == 3
assert y == 4
assert type((5,)) is tuple
assert type((5)) is int
assert type(("a", "b")) is tuple

first = 1
second = 2
first, second = second, first
assert first == 2
assert second == 1
grades = (80, 95, 80, 70)
assert grades.count(80) == 2
assert grades.index(95) == 1
assert 70 in grades
assert grades + (100,) == (80, 95, 80, 70, 100)

Common mistakes

  1. Trying to change an item. The code point = (3, 4) followed by point[0] = 9 gives:

    TypeError: 'tuple' object does not support item assignment
    

    Create a new tuple instead, such as point = (9, point[1]). If you need to change items often, use a list.

  2. Forgetting the comma in a one-item tuple. The code value = (5) followed by value[0] gives:

    TypeError: 'int' object is not subscriptable
    

    Write value = (5,) with a trailing comma.

  3. Unpacking with the wrong count. The code x, y = (1, 2, 3) gives:

    ValueError: too many values to unpack (expected 2)
    

    The number of names on the left must match the number of items on the right.

  4. Unpacking a number. The code x, y = 5 gives:

    TypeError: cannot unpack non-iterable int object
    

    Only collections such as tuples and lists can be unpacked. Check the type of the value on the right.

Practice

  1. Create city = ("Paris", 2161). Use indexing to assert that city[0] is "Paris", and assert that len(city) is 2.
  2. Create pair = (7, 9). Unpack it into left and right, then assert that left + right equals 16.
  3. Create letters = ("a", "b", "a", "c"). Assert that letters.count("a") is 2 and that letters.index("c") is 3.
  4. Create one_item = ("hello",). Assert that type(one_item) is tuple and that len(one_item) is 1.
  5. Stretch: start with x = 1 and y = 2, and a tuple coords = (x, y). Swap the values of x and y with a single unpacking line, then assert that coords still equals (1, 2) and that x is now 2 and y is now 1.
Solution
  1. python city = ("Paris", 2161) assert city[0] == "Paris" assert len(city) == 2

  2. python pair = (7, 9) left, right = pair assert left + right == 16

  3. python letters = ("a", "b", "a", "c") assert letters.count("a") == 2 assert letters.index("c") == 3

  4. python one_item = ("hello",) assert type(one_item) is tuple assert len(one_item) == 1

  5. python x = 1 y = 2 coords = (x, y) x, y = y, x assert coords == (1, 2) assert x == 2 assert y == 1

Recap

  • A tuple is an immutable, ordered sequence. Its items are read by index, but they cannot be changed.
  • The comma makes a tuple. A one-item tuple needs a trailing comma, as in (5,).
  • Unpacking, such as a, b = b, a, gives names to the items in one line and makes swaps straightforward.

Sources