Skip to content
derpx06Notes on systems, models & learning
3. Data Structures · lesson 6 of 17 · 1 min · January 1, 2024

Tuples

Tuples are lists' immutable cousins.

Tuples are lists’ immutable cousins. Pretty much anything you can do to a list that doesn’t involve modifying it, you can do to a tuple. You specify a tuple by using parentheses (or nothing) instead of square brackets:

tuples.py
my_list = [1, 2]
my_tuple = (1, 2)
other_tuple = 3, 4
my_list[1] = 3      # my_list is now [1, 3]

try:
  my_tuple[1] = 3
except TypeError:
  print "cannot modify a tuple"

Tuples are a convenient way to return multiple values from functions:

multiple_returns.py
def sum_and_product(x, y):
  return (x + y),(x * y)

sp = sum_and_product(2, 3)            # equals (5, 6)
s, p = sum_and_product(5, 10)         # s is 15, p is 50

Tuples (and lists) can also be used for multiple assignment:

swapping.py
x, y = 1, 2
x, y = y, x     # Pythonic way to swap variables; now x is 2, y is 1