Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A tuple is a built-in Python data structure that represents an ordered, immutable sequence of elements. Tuples can store heterogeneous data types and are hashable if all their constituent elements are also hashable. Because they are immutable, their size and element references are fixed at the time of creation.

Syntax and Creation

Tuples are typically defined using parentheses (), with elements separated by commas. However, the comma is the actual tuple-defining operator; the parentheses are only required for empty tuples or to avoid syntactic ambiguity.

# Standard initialization
empty_tuple = ()
standard_tuple = (1, "string", 3.14)


# Single-element tuple requires a trailing comma
single_element = (42,)
not_a_tuple = (42)  # Evaluates to an integer, not a tuple


# Implicit creation (Tuple Packing)
packed_tuple = 1, 2, 3


# Constructor from an iterable
iterable_to_tuple = tuple([1, 2, 3])

Immutability Mechanics

Immutability in tuples applies to the references held by the tuple, not necessarily the underlying objects. You cannot add, remove, or reassign elements. However, if a tuple contains a reference to a mutable object (like a list), the internal state of that mutable object can still be modified.
t = (1, [2, 3], 4)


# Attempting to reassign a reference raises a TypeError

# t[0] = 10  # TypeError: 'tuple' object does not support item assignment


# Modifying a mutable object referenced by the tuple is permitted
t[1].append(99)

# t evaluates to: (1, [2, 3, 99], 4)

Indexing and Slicing

Tuples support standard Python sequence operations, including zero-based indexing, negative indexing, and slicing. Slicing a tuple always returns a new tuple object.
t = ('a', 'b', 'c', 'd', 'e')


# Indexing
first_item = t[0]        # 'a'
last_item = t[-1]        # 'e'


# Slicing [start:stop:step]
slice_tuple = t[1:4]     # ('b', 'c', 'd')
reversed_tuple = t[::-1] # ('e', 'd', 'c', 'b', 'a')

Unpacking

Tuple unpacking allows the extraction of tuple elements into distinct variables in a single operation. Python also supports extended unpacking using the * (iterable unpacking) operator to capture remaining elements into a list.
t = (10, 20, 30)
x, y, z = t  # x=10, y=20, z=30


# Extended unpacking
t2 = (1, 2, 3, 4, 5)
first, *middle, last = t2

# first = 1

# middle = [2, 3, 4]

# last = 5

Built-in Methods

Because tuples cannot be modified in place, they lack the mutation methods found in lists (e.g., append(), remove()). They expose only two built-in methods:
t = (1, 2, 2, 3, 1, 1)


# count(value): Returns the integer count of occurrences of 'value'
ones_count = t.count(1)  # Returns 3


# index(value): Returns the zero-based index of the first occurrence of 'value'
first_two_idx = t.index(2)  # Returns 1

Memory and Performance Characteristics

Under the hood, CPython implements tuples as arrays of pointers. Because their length is immutable, Python allocates the exact memory block required for the elements at creation. This contrasts with lists, which are dynamic arrays that utilize over-allocation to optimize future append() operations. Consequently, tuples have a strictly smaller memory footprint than lists of the same length and offer slightly faster iteration and instantiation times.
Master Python with Deep Grasping Methodology!Learn More