Skip to content

Module 8 · Python Data Structures — topic 10 of 22

Tuples

Tuples are ordered, immutable sequences used for fixed records and multiple values. Learn tuple syntax including the one-item tuple, why immutability is useful, and where Python creates tuples for you.

Beginner 10 min read·Lesson 113 of 306 in Python Programming

Learning objectives

  • Create tuples including empty and single-item tuples
  • Explain when a tuple is better than a list
  • Recognise tuples created by packing and functions

Definition

Tuples are ordered, immutable sequences used for fixed records and multiple values. Learn tuple syntax including the one-item tuple, why immutability is useful, and where Python creates tuples for you.

Introduction

A tuple is like a list that can’t be changed. It is written with parentheses and commas: ("Asha", 82, "Pune"). Once created, its items can’t be replaced, added or removed.

Why it matters

Immutability is a feature. A tuple communicates “this is a fixed record”, such as a coordinate, a date or an RGB colour, and protects it from accidental changes. Tuples can also be dictionary keys and set members, which lists can’t.

Detailed explanation

The commas make the tuple, not the parentheses: t = 1, 2, 3 is also a tuple. Parentheses are needed for clarity and inside larger expressions.

An empty tuple is (). A single-item tuple needs a trailing comma: (5,). Without it, (5) is just the number 5 in parentheses.

Tuples support indexing, slicing, len(), in, iteration, + and * like other sequences.

They have only two methods: count() and index().

Python creates tuples automatically when you write several values separated by commas: return a, b returns a tuple, and x, y = y, x swaps through a temporary tuple.

Tuples are usually slightly smaller and faster than lists, but the main reason to choose one is meaning: a fixed group of related values.

A tuple can contain mutable items such as a list; the tuple can’t be changed, but the list inside it can.

Syntax

Python
point = (12.5, 77.3)
empty = ()
single = (5,)
packed = "Asha", 82
  • Commas create the tuple.
  • A one-item tuple needs a trailing comma.
  • Values separated by commas are packed into a tuple.

Code examples

Creating tuples

Python
point = (12.5, 77.3)
single = (5,)
not_a_tuple = (5)
packed = "Asha", 82
print(type(point).__name__, type(single).__name__, type(not_a_tuple).__name__)
print(packed, len(packed))
Output
tuple tuple int
('Asha', 82) 2

Immutability

Python
colour = (255, 128, 0)
print(colour[0], colour[-1])
try:
colour[0] = 200
except TypeError as err:
print("TypeError:", err)
Output
255 0
TypeError: 'tuple' object does not support item assignment

More examples

Tuples as records and dictionary keys

Python
def min_max(values):
return min(values), max(values)
low, high = min_max([72, 45, 88, 91])
print(low, high)
distances = {("Pune", "Mumbai"): 150, ("Delhi", "Agra"): 230}
print(distances[("Delhi", "Agra")])
try:
bad = {["Pune", "Mumbai"]: 150}
except TypeError:
print("Lists can't be keys; tuples can")
record = ("Asha", [82, 91])
record[1].append(78)
print(record)
Output
45 91
230
Lists can't be keys; tuples can
('Asha', [82, 91, 78])

Step-by-step explanation

  • min_max returns two values, which Python packs into a tuple.
  • low, high = ... unpacks that tuple into two names.
  • A pair of cities is a natural tuple key for a distance lookup.
  • The tuple record can’t be reassigned, but the list inside it can still change.

Expected output

Running “Tuples as records and dictionary keys” prints exactly:

TextOutput
45 91
230
Lists can't be keys; tuples can
('Asha', [82, 91, 78])

Practical applications

  • Map applications store coordinates as (latitude, longitude) tuples.
  • Databases return rows as tuples.
  • Functions return several results as a tuple, such as divmod().

Best practices

  • Use tuples for fixed-size records whose fields have meanings.
  • Use lists for collections that grow or change.
  • Unpack tuples into well-named variables.

Common mistakes

Forgetting the comma in a single-item tuple

Avoid

options = ("yes")

Prefer

options = ("yes",)

Without the comma it is just a string in parentheses.

Trying to append to a tuple

Avoid

point.append(5)

Prefer

point = point + (5,)

Tuples have no append; build a new tuple if you must.

Key takeaways

  • Tuples are ordered and immutable.
  • Commas create tuples; (x,) is a single-item tuple.
  • Tuples can be dictionary keys and are returned for multiple values.

Try it yourself

  1. 1
    Store a date as a (day, month, year) tuple and print it in day/month/year form.
  2. 2
    Write a function that returns the sum and average of a list as a tuple, and unpack the result.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic