Tuple

An ordered, unchangeable sequence of values, written in parentheses, e.g. (3, 4).

A tuple is like a list, but immutable — once created, its items cannot be changed. You write one in parentheses (or just commas), index it like a list, and unpack it into several variables at once. Tuples are ideal for a fixed group of values, like an (x, y) coordinate or the several results a function returns.

Example
point = (3, 4)          # a 2-item tuple
print(point[0])         # index like a list
x, y = point            # unpack into two variables
print(x, y)
# point[0] = 9          # would raise: tuples cannot be changed
Output
3
3 4

Where this shows up in real Python

Tuples show up as coordinates, database rows, dictionary keys, and any time a function returns several values at once.

Commonly used Tuple tools

  • (1, 2, 3) — create a tuple with parentheses
  • x, y = point — unpack into variables
  • point[0] — index like a list
  • len(point), item in point — length and membership
  • tuple([1, 2]) — convert a list to a tuple

Official documentation: Python Library Reference: Tuples

Related lessons

Related terms