Tuples

Published by Brayan K

A tuple is an ordered collection like a list — but frozen. Once you create it, it can never change. That sounds restrictive, yet it's exactly what you want for fixed records: a coordinate, an RGB color, a database row, a function returning two values at once.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

The comma makes the tuple

A tuple is written as values separated by commas, usually inside parentheses. You read it the way you read a list, with an index, len() and in:

point = (3, 4)
person = ("Ada", 36, "London")
print(point, person[0], len(person), "London" in person)

also = 1, 2, 3
print(also, type(also))

# ✅ Expected output:
# (3, 4) Ada 3 True
# (1, 2, 3) <class 'tuple'>

The parentheses only group, as they do in arithmetic; the commas make the tuple. That matters most for a tuple of one item, which needs a comma after it:

fruits = ("apple")
for f in fruits:
    print(f)

# ✅ Expected output:
# a
# p
# p
# l
# e

("apple") is just the string, so the loop went through its letters. With the comma it's a tuple of one item, and that's why such a tuple prints with a comma after it: ('apple') would read back as a plain string.

fruits = ("apple",)
for f in fruits:
    print(f)
print(fruits, len(fruits))

# ✅ Expected output:
# apple
# ('apple',) 1

The same slip with a number fails louder: after sizes = (5), for s in sizes: stops with TypeError: 'int' object is not iterable. The opposite slip, a stray comma at the end of a line, turns a value into a tuple without a word:

total = 5,
print(total + 1)

# ✅ Expected error:
# TypeError: can only concatenate tuple (not "int") to tuple

A tuple's items can't be replaced

Reading is fine; assigning to a position isn't:

coords = (51.5, -0.12)
coords[0] = 52.0

# ✅ Expected error:
# TypeError: 'tuple' object does not support item assignment

The list methods that change a list don't exist: coords.append(1) stops with AttributeError: 'tuple' object has no attribute 'append', as *args & **kwargs showed, and del coords[0] with TypeError: 'tuple' object doesn't support item deletion. A tuple has two methods, count() and index(). To get a changed tuple, build a new one:

nums = (1, 2, 2, 3)
print(nums.count(2), nums.index(3))

nums = nums + (4,)
print(nums)

# ✅ Expected output:
# 2 3
# (1, 2, 2, 3, 4)

nums + (4,) makes a new tuple and the name nums moves to it. The old tuple never changed, which is how numbers and text behave too, as Variables and Data Types showed.

Frozen slots, not frozen contents

"It can never change" is about the tuple's own slots: which values it holds, in which order. A value in a slot can still change if it's the kind that can, such as a list:

record = ("Ada", [90, 85])
record[1].append(70)
print(record)

# ✅ Expected output:
# ('Ada', [90, 85, 70])

The tuple still holds the same string and the same list; only the list grew. That matters for keys: a tuple is hashable only when everything in it is. hash(), which Dictionaries used on a list, shows it:

record = ("Ada", [90, 85])
print(hash(("Ada", 90)) == hash(("Ada", 90)))
print(hash(record))

# ✅ Expected output:
# True

# ✅ Expected error:
# TypeError: unhashable type: 'list'

So that tuple can't be a dictionary key or go in a set: {record: "A"} stops with the same TypeError, which Python 3.14 words cannot use 'tuple' as a dict key (unhashable type: 'list'). A tuple of strings and numbers is hashable, which is why Dictionaries keys places by a (latitude, longitude) tuple and Sets accepts tuples where it refuses lists.

Unpacking, in assignments and in loops

Unpacking gives each item of a tuple its own name, the way Variables and Data Types unpacked x, y = 1, 2. A for loop can do it on every item of a list of tuples:

point = (3, 4)
x, y = point
print(x, y)

rows = [("Ada", 88), ("Ben", 72)]
for name, score in rows:
    print(name, score)

# ✅ Expected output:
# 3 4
# Ada 88
# Ben 72

The number of names has to match the number of items, in a loop as anywhere else:

rows = [("Ada", 88), ("Ben", 72)]
for name, score, grade in rows:
    print(name, score, grade)

# ✅ Expected error:
# ValueError: not enough values to unpack (expected 3, got 2)

Too many items gives the opposite: x, y = (1, 2, 3) stops with ValueError: too many values to unpack (expected 2), and Python 3.14 adds how many it got, (expected 2, got 3). A starred name, as in first, *rest = rows, collects whatever is left over, which Unpacking & Star Expressions covers, and enumerate & zip hands a loop pairs like these to unpack.

Returned pairs, and comparing tuples

A function that returns a, b returns one tuple, which is why the pair in Functions printed in parentheses. And tuples compare item by item, from the left:

def min_max(nums):
    return min(nums), max(nums)

result = min_max([4, 9, 2, 7])
print(result, type(result))

print((1, 2) == [1, 2], (2, 0) > (1, 99))
print(sorted([(2, "b"), (1, "z"), (2, "a")]))

# ✅ Expected output:
# (2, 9) <class 'tuple'>
# False True
# [(1, 'z'), (2, 'a'), (2, 'b')]

A tuple never equals a list, even with the same items. (2, 0) is bigger than (1, 99) because the first items already differ; only a tie there moves the comparison to the second item. So sorting tuples sorts by the first item, then the second, the idea Sorting with sorted() & key builds on. For records with many fields, a name per field reads better than an index: namedtuple & NamedTuple adds them.

Practice: the midpoint of two points

Fill the three blanks with the values in the hint lines, in order: unpack both points, then check the midpoint against the tuple it should equal.

p1 = (2, 2)
p2 = (4, 6)
x1, y1 = ___
x2, y2 = ___
mid = ((x1 + x2) / 2, (y1 + y2) / 2)
print(mid)
print(mid ___ (3.0, 4.0))

# 👉 replace ___ with p1
# 👉 replace ___ with p2
# 👉 replace ___ with ==

# ✅ Expected output:
# (3.0, 4.0)
# True

On your own: a seat map

A theatre lists bookings as (name, seat) pairs, where each seat is a (row, number) tuple: bookings = [("Ada", ("B", 4)), ("Ben", ("A", 1)), ("Cleo", ("B", 5))]. Store them in a dictionary keyed by seat, print how many seats are taken and who has B4, then go through the taken seats in row order and print each one's row, number and name, starting with A 1 Ben. Then compare with this solution:

bookings = [("Ada", ("B", 4)), ("Ben", ("A", 1)), ("Cleo", ("B", 5))]

taken = {}
for name, seat in bookings:
    taken[seat] = name

print(len(taken))
print(taken[("B", 4)])
for seat in sorted(taken):
    row, number = seat
    print(row, number, taken[seat])

# ✅ Expected output:
# 3
# Ada
# A 1 Ben
# B 4 Ada
# B 5 Cleo

The seats can be keys because a tuple of a string and a number is hashable, and sorted(taken) orders them by row first and number second, because that's how tuples compare.

Practice quiz

x = (5). What is type(x)?

  • tuple
  • int
  • list
  • An error

Answer: int. Parentheses only group. Without a comma, (5) is just the number 5.

How do you write a tuple holding only the number 5?

  • (5)
  • [5]
  • (5,)
  • {5}

Answer: (5,). The trailing comma is what makes it a tuple.

t = (1, 2, 3), then t[0] = 9. What happens?

  • t becomes (9, 2, 3)
  • It stops with TypeError: 'tuple' object does not support item assignment
  • It returns 9
  • Nothing

Answer: It stops with TypeError: 'tuple' object does not support item assignment. A tuple's items can't be replaced.

r = ("Ada", [90]), then r[1].append(70). What is r?

  • It stops with a TypeError
  • ('Ada', [90])
  • ('Ada', [90, 70])
  • It stops with an AttributeError

Answer: ('Ada', [90, 70]). The tuple still holds the same list, and that list can change.

What does name, score = ("Ada", 88, "A") do?

  • It stops with a ValueError: too many values to unpack
  • name is 'Ada' and score is 88
  • score is (88, 'A')
  • It stops with a TypeError

Answer: It stops with a ValueError: too many values to unpack. Three values can't be unpacked into two names.

What does (1, 2) == [1, 2] give?

  • True
  • An error
  • None
  • False

Answer: False. A tuple never equals a list, even with the same items.

Which of these can be a dictionary key?

  • (1, 2)
  • [1, 2]
  • (1, [2])
  • {1, 2}

Answer: (1, 2). A tuple is hashable when everything in it is. (1, [2]) holds a list, so it isn't.

def f(): return 1, 2. What is type(f())?

  • int
  • list
  • Two separate values
  • tuple

Answer: tuple. return 1, 2 returns one tuple, (1, 2).

Continue this course

Frequently asked questions

Why use a tuple instead of a list?

When the collection is one fixed record: a coordinate, an RGB colour, a (name, score) row. A tuple can't be changed by accident, and a tuple of hashable values can be a dictionary key or go in a set, which a list never can. Use a list for a collection that grows, shrinks or gets sorted in place.

Is a tuple smaller or faster than a list?

A tuple of the same items takes a little less memory, but the difference rarely matters. Choose by meaning: a tuple for one fixed record, a list for a collection that changes. Memory Management & Garbage Collection Internals covers how Python stores objects.

Related lessons