Codectionary / Developer documentation / Python

Tuples

A tuple is an ordered, immutable collection, created with parentheses (or often just commas). Once created, a tuple's contents cannot be changed, added to, or removed - this immutability makes tuples faster than lists and safe to use as dictionary keys or in sets. Tuples are commonly used for fixed collections of related values, like coordinates or RGB colors, and for returning multiple values from a function.

Syntax

(item1, item2, item3)

Examples

Creating Tuples

Basic tuple creation and access, identical in syntax to lists except for the brackets used.

point = (3, 4)
colors = ("red", "green", "blue")

print(point[0])       # 3
print(colors[-1])     # blue

single = (5,)  # a comma is required for a one-item tuple
print(type(single))   # <class 'tuple'>

Tuple Unpacking

Assigning each tuple element to its own variable in one line.

point = (3, 4)
x, y = point
print(x, y)  # 3 4

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

low, high = get_min_max([4, 1, 9, 2])
print(f"Min: {low}, Max: {high}")

Immutability

Attempting to modify a tuple raises an error - this is by design.

point = (3, 4)

try:
    point[0] = 10
except TypeError as e:
    print(f"Error: {e}")

# To "change" a tuple, create a new one instead
point = (10, point[1])
print(point)  # (10, 4)

Named Tuples

Using collections.namedtuple for tuples with readable, named fields.

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)

print(p.x, p.y)   # 3 4
print(p[0], p[1]) # 3 4 (still works like a regular tuple)

Best practices

  • Use tuples for fixed collections that should never change, and lists when the contents need to grow or be reordered
  • Don't forget the trailing comma when creating a single-element tuple - (5) is just the number 5, not a tuple
  • Use tuple unpacking to return and receive multiple values from a function cleanly
  • Reach for namedtuple (or a dataclass) when a plain tuple starts feeling unclear about what each position means
  • Tuples can be used as dictionary keys because they are immutable and hashable - lists cannot

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

Lists
A list is Python's built-in ordered, mutable collection type, created with square brackets. Lists can hold items of any type - even a mix of types - and support indexing, slicing, and a wide range of built-in methods for adding, removing, and reordering elements. Because they are mutable, lists can be changed in place after creation, which makes them the go-to structure for collections that grow or shrink over time.
Dictionaries
A dictionary stores data as key-value pairs, created with curly braces. Keys must be unique and hashable (strings, numbers, or tuples are common choices), while values can be anything, including other dictionaries or lists. Since Python 3.7, dictionaries maintain insertion order. They are one of the most heavily used data structures in Python, ideal for representing structured records, lookups, and mappings.
Sets
A set is an unordered collection of unique, hashable items, created with curly braces or the set() function. Sets automatically eliminate duplicates and support fast membership testing, along with mathematical set operations like union, intersection, and difference. They're especially useful for deduplicating data and for comparing two collections to find overlaps or differences.
List Comprehensions
A list comprehension is a concise, expressive way to build a new list by applying an expression to every item in an iterable, optionally filtering with a condition. It replaces the common pattern of creating an empty list and appending to it inside a for loop, condensing that logic into a single readable line. Comprehensions are considered idiomatic Python for simple to moderately complex transformations.