Tuples and Unpacking

Python 3.12 · ✓ verified by execution on 2026-07-22

A tuple is a collection of items similar to a list. However, while lists are designed to be changed (mutable), tuples are permanently fixed (immutable).

You create a tuple by separating values with commas. Parentheses are usually added to group the items clearly.

python
coordinates = (10, 20)
print(coordinates)
print(type(coordinates))
Output
(10, 20)
<class 'tuple'>

Because tuples are immutable, you can never alter their contents once created. There is no append(), no remove(), and you cannot overwrite an index.

python
try:
    coordinates = (10, 20)
    coordinates[0] = 15
except TypeError as e:
    print(repr(e))
Output
TypeError("'tuple' object does not support item assignment")

The Empty Tuple

Creating an empty tuple is straightforward: you use an empty pair of parentheses.

python
empty = ()
print(type(empty))
print(len(empty))
Output
<class 'tuple'>
0

The Single Element Gotcha

Here is one of Python’s most famous quirks: creating a tuple with exactly one element requires a trailing comma.

python
not_a_tuple = (5)
real_tuple = (5,)
print(type(not_a_tuple))
print(type(real_tuple))
Output
<class 'int'>
<class 'tuple'>

Without the comma, Python treats (5) as an integer wrapped in mathematical parentheses!

Tuple Unpacking

Tuples allow you to extract all their elements directly into variables in a single step. This is known as unpacking.

python
user = ('Alice', 25, 'Admin')
name, age, role = user
print(name)
print(age)
print(role)
Output
Alice
25
Admin

When unpacking, you must provide exactly one variable for every item in the tuple. If the counts don’t match, Python throws a ValueError.

python
try:
    user = ('Alice', 25, 'Admin')
    name, age = user
except ValueError as e:
    print(repr(e))
Output
ValueError('too many values to unpack (expected 2, got 3)')

The Swapping Idiom

Because Python evaluates the entire right side of an assignment before assigning it to the left side, you can use tuple packing and unpacking to swap two variables on a single line!

python
a = 'apple'
b = 'banana'
a, b = b, a
print(a, b)
Output
banana apple

Behind the scenes, Python packs b and a into a temporary tuple, and immediately unpacks it into the a and b variables on the left.

Returning Multiple Values

A function in Python can technically only return exactly one item. However, if you return multiple values separated by commas, Python automatically packs them into a single tuple!

python
def get_min_max(numbers):
    return min(numbers), max(numbers)

lowest, highest = get_min_max([10, 2, 7, 9])
print(lowest)
print(highest)
Output
2
10

This pattern makes Python functions incredibly flexible, allowing you to return and immediately unpack multiple pieces of data.

Check yourself

Parentheses are required to create a tuple.

Reveal answer

False, commas create the tuple. Parentheses are just often used for grouping. — Commas create the tuple, not parentheses (except for the empty tuple). Parentheses are just often used for grouping.

Does (5) create a tuple with one element?

Reveal answer

No, it creates a standard integer. You must use (5,) — Without a trailing comma, Python evaluates (5) as a standard mathematical grouping, resulting in an integer. You must use (5,).

Can you change a tuple's contents if you use list methods?

Reveal answer

No, tuples have no append(), insert(), or pop() methods because they are completely immutable. — Tuples have no append(), insert(), or pop() methods precisely because they are completely immutable.

Challenges

Challenge 1 +15 XP

You are given two variables, 'first' and 'second'. Use Python's tuple packing and unpacking to swap their values in a single line of code. Do not use a temporary variable.

python
  • Test 1 — expects "200\n100\n"
Need a hint? (−25% XP)

Put both variables on the left side of the equals sign separated by a comma, and their reversed order on the right side.

Show solution (0 XP)
first = 100
second = 200
first, second = second, first
print(first)
print(second)

Challenge 2 +20 XP

Write a function 'get_dimensions' that takes a list of numbers and returns both the first number (width) and the second number (height). Then call the function and unpack the result into two variables: w and h.

python
  • Test 1 — expects "1920 x 1080\n"
Need a hint? (−25% XP)

Use 'return nums[0], nums[1]' in the function. When calling it, use 'w, h = get_dimensions([1920, 1080])'.

Show solution (0 XP)
def get_dimensions(nums):
    return nums[0], nums[1]

w, h = get_dimensions([1920, 1080])
print(w, 'x', h)