🐍 Python Course

Tuples

1.3 Tuples

Fixed-size data, unpacking, and immutability

Tuples are one of Python’s most important built-in sequence types.

At first glance, they look similar to lists, but the key difference is:

a tuple is immutable

That single difference changes how they are used in interviews, backend code, and API design.

Tuples are especially common in:

  • returning multiple values from functions
  • coordinates and fixed-size records
  • dictionary keys
  • unpacking values
  • iteration patterns
  • function arguments

This chapter focuses on understanding why tuples exist and when they are the better choice over lists.


1.3.1 Mental model: what a tuple really is

A tuple is an ordered, immutable sequence.

Example

point = (10, 20)

This means two important things:

Ordered

Every element has a stable position.

point[0]   # 10
point[1]   # 20

Just like lists.

Immutable

Unlike lists, tuples cannot be changed after creation.

This will fail:

point[0] = 99

Error:

TypeError: 'tuple' object does not support item assignment

This is the defining feature of tuples.


1.3.2 Syntax

Standard syntax

coords = (4, 7)

Important trap: single-element tuple

This is extremely commonly tested.

x = (5)

This is not a tuple.

It is just an integer in parentheses.

Type:

int

Correct single-element tuple

x = (5,)

The comma is what makes it a tuple.

This is a classic interview trap.


1.3.3 Why tuples exist

A strong interviewer may ask:

Why use tuple instead of list?

Strong answer:

  • fixed-size structure
  • immutable
  • safer semantic meaning
  • can be hashable
  • communicates intent clearly

Example

rgb = (255, 0, 0)

This clearly means a fixed record.

Using a list would suggest mutability.


1.3.4 Tuple unpacking

This is one of the most important tuple topics.

Basic unpacking

point = (10, 20)
x, y = point

Now:

x = 10
y = 20

This is called unpacking.

Python assigns each element to a variable.

Why this is important

Tuple unpacking appears everywhere in real code.

Example

name, age = ("Ruben", 30)

Very common.

Function return unpacking

This is one of the biggest tuple use cases.

def get_user():
    return "Ruben", 30

name, age = get_user()

This is extremely common in Python.

Technically the function returns a tuple.


1.3.5 Swapping variables

A very famous Python tuple pattern.

a = 1
b = 2

a, b = b, a

Now:

a = 2
b = 1

This is elegant and highly interview-relevant.

Behind the scenes, Python uses tuple packing and unpacking.


1.3.6 Extended unpacking

Medior-level topic and often asked.

nums = (1, 2, 3, 4)

first, *middle, last = nums

Result

first = 1
middle = [2, 3]
last = 4

Very important to know.

Notice:

middle becomes a list, not a tuple.

That is an important subtlety.


1.3.7 Immutability in depth

This is the key concept.

Once created, the tuple structure cannot change.

You cannot:

  • assign by index
  • append
  • remove
  • sort in place

Example

point.append(3)

Error:

AttributeError

Important nuance: nested mutables

This is a strong interview nuance.

A tuple itself is immutable, but it may contain mutable objects.

Example

data = ([1, 2], [3, 4])

This is valid.

Now:

data[0].append(99)

This works.

Result

([1, 2, 99], [3, 4])

Important insight:

  • the tuple structure is immutable
  • the objects inside may still be mutable

This is a strong medior-level answer.


1.3.8 Hashability and dictionary keys

This is extremely important.

Tuples can be dictionary keys if all elements are hashable.

Example

locations = {
    (10, 20): "home"
}

This is valid.

Why list cannot be key

{
    [10, 20]: "home"
}

This fails because lists are mutable and not hashable.

This comparison is frequently tested.


1.3.9 Important tuple methods

Tuples have fewer methods than lists.

The main ones:

t.count(2)
t.index(5)

Important verbal point:

Tuples are intentionally minimal because they are immutable.


1.3.10 Common interview use cases

Tuples often appear in:

  • coordinates
  • matrix positions
  • multiple return values
  • sorting keys
  • dictionary keys
  • caching

Examples:

(row, col)
(name, age)
(key, value)

This chapter is foundational.


Verbal interview questions

Answer these out loud:

  • Why use tuple instead of list?
  • Why can tuple be a dictionary key?
  • Explain unpacking
  • Explain extended unpacking
  • How can a tuple still contain mutable data?

Coding drills

Drill 1: swap values

def swap(a, b):
    ...

Use tuple unpacking.

Drill 2: return min and max

def min_max(nums: list[int]) -> tuple[int, int]:
    ...

Return both values as a tuple.

Drill 3: coordinate map

points = {
    (0, 0): "origin",
    (1, 2): "point"
}

Explain why tuple works as key.