LearnFrenzy
Sign in Take a free test

Lists and Tuples · Lesson 18 of 39

Tuples and Unpacking

4 min read Updated Nov 7, 2026 Saurabh Samir

A tuple is a list that cannot be changed. That sounds like a limitation, but it is exactly what you want for values that belong together and should stay fixed: a date, GPS coordinates, an RGB colour, a database row.

In this lesson

  • Creating tuples (and the single-item trap)
  • Why immutability is useful
  • Unpacking, including *rest
  • Tuple vs list and named tuples

Creating a tuple

point = (12.97, 77.59)          # latitude, longitude
rgb = (255, 128, 0)
date = 2026, 11, 7              # brackets are optional
empty = ()

print(point[0], rgb[-1], date)
print(type(date), len(rgb))

Output

12.97 0 (2026, 11, 7)
<class 'tuple'> 3

It is the comma that makes a tuple, not the brackets. That explains the strangest rule in Python:

not_a_tuple = ("solo")
one_item = ("solo",)
print(type(not_a_tuple).__name__, type(one_item).__name__)

Output

str tuple

Common mistake: the missing comma

A single-item tuple needs a trailing comma: ("solo",). Without it, the brackets are just grouping and you get a plain string or number.

Immutability

rgb = (255, 128, 0)
try:
    rgb[0] = 100
except TypeError as error:
    print("Error:", error)

rgb = (100,) + rgb[1:]    # build a new tuple instead
print(rgb)

Output

Error: 'tuple' object does not support item assignment
(100, 128, 0)

Why is that useful?

  • Safety – data you pass around can't be changed by accident.
  • Dictionary keys – tuples (of immutable items) can be keys and set members; lists can't.
  • Intent – a tuple tells the reader "this is a fixed record".
  • Slightly faster and smaller than lists.
distances = {("Delhi", "Agra"): 233, ("Mumbai", "Pune"): 148}
print(distances[("Mumbai", "Pune")])

Output

148

Tuples have only two methods, count() and index(), and support in, len(), slicing and looping like any sequence.

Unpacking

Unpacking assigns each item of a tuple (or any sequence) to its own name in one line:

employee = ("E104", "Farah", "Design", 64000)
emp_id, name, team, salary = employee
print(name, "in", team)

Output

Farah in Design

emp_id, name, team, salary = employee

  • emp_id"E104"item 0
  • name"Farah"item 1
  • team"Design"item 2
  • salary64000item 3

The number of names must match the number of items, otherwise you get ValueError: too many values to unpack. A starred name collects "the rest":

first, *middle, last = [10, 20, 30, 40, 50]
print(first, middle, last)

head, *tail = "python"
print(head, tail)

_, name, *_ = ("E104", "Farah", "Design", 64000)   # ignore what you don't need
print(name)

Output

10 [20, 30, 40] 50
p ['y', 't', 'h', 'o', 'n']
Farah

Returning several values

A function that "returns two values" really returns one tuple, which the caller unpacks:

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

low, high = min_max([7, 3, 9, 1])
print(low, high)
print(min_max([5, 8]))

Output

1 9
(5, 8)

The swap trick a, b = b, a from chapter 2 works the same way: the right side builds a tuple, the left side unpacks it.

Named tuples: readable records

employee[3] is not very descriptive. A namedtuple lets you use field names while keeping all the benefits of a tuple:

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x, p.y)
print(p)
print((p.x ** 2 + p.y ** 2) ** 0.5)

Output

3 4
Point(x=3, y=4)
5.0

For records that need defaults or methods, chapter 12 introduces dataclasses.

Tuple or list?

TupleList
Syntax(1, 2, 3)[1, 2, 3]
Can change?No (immutable)Yes (mutable)
Typical contentfixed record of different fieldsmany items of the same kind
Dictionary key?YesNo
Example("Farah", "Design", 64000)["Farah", "Ben", "Om"]

Practice

Try it yourself

  1. Given scores = [("Ira", 82), ("Jay", 91), ("Kim", 77)], print each name and score using unpacking in the loop.

    Show solution
    scores = [("Ira", 82), ("Jay", 91), ("Kim", 77)]
    for name, score in scores:
        print(f"{name}: {score}")

    Output

    Ira: 82
    Jay: 91
    Kim: 77
  2. Write a function that returns the quotient and remainder of two numbers, and unpack the result.

    Show solution
    def divide(a, b):
        return a // b, a % b
    
    q, r = divide(47, 5)
    print(q, r)

    Output

    9 2

Quick check

What is type((5))?

int. You need (5,) for a tuple.

What does a, *b = [1, 2, 3] assign to b?

[2, 3] – starred names always become lists.

Why can a tuple be a dictionary key but a list can't?

Keys must be hashable, which requires immutability. Tuples (of immutable items) are; lists are not.

Summary

  • A tuple is an immutable sequence; the comma makes it, (x,) for one item.
  • Unpack with a, b, c = t and collect leftovers with *rest.
  • Functions return several values as a tuple.
  • Use tuples for fixed records and keys, lists for collections that change.

Ready for interview questions on collections? Try the Python data structures set.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Popular