Immutable heterogeneous records
Representing a fixed record such as a name-and-value pair without allowing the container structure to change.
Python · Class
tuple is Python’s immutable sequence type. Create tuples with comma-separated values or tuple(iterable); they support common sequence operations and can hold heterogeneous or homogeneous data.
tuple is an immutable sequence type. You can create one from an iterable, preserve the iterable's item order, access elements with normal sequence operations, and use comma-separated literals; the comma, rather than parentheses alone, determines tuple construction.
class tuple(iterable=(), /)| Name | Type | Description |
|---|---|---|
iterable | iterable | Optional sequence, iterable container, or iterator whose items are copied into the resulting tuple; defaults to an empty tuple. |
A tuple containing the iterable's items in the same order; with no argument, an empty tuple.
tuple(iterable) preserves both the elements and their iteration order.(item,); (item) is just the parenthesized item.tuple() does not create a different tuple value.values = tuple([1, 2, 3])
record = ("Ada", 42)
singleton = ("ready",)
print(values)
print(record[0], record[1])
print(singleton)This runnable example creates tuples from an iterable and literals, including the required trailing comma for a one-item tuple.
Representing a fixed record such as a name-and-value pair without allowing the container structure to change.
Converting a sequence, iterable container, or iterator into an ordered immutable sequence.
Using a fixed collection where immutability is useful for membership in sets or use as dictionary keys, subject to the contents being hashable.
tuple is an immutable sequence type, so its sequence structure cannot be changed after creation. It is commonly used for heterogeneous records or immutable homogeneous sequences.
Follow-up: Why can a tuple still contain mutable state? Because immutability applies to the tuple's item references, not necessarily to the objects referenced by those items.
tuple() construct its result?The constructor is tuple(iterable=(), /). With no argument it creates an empty tuple; with an iterable it creates a tuple containing the iterable's items in the same order.
Follow-up: What happens when the argument is already a tuple? The documentation says it is returned unchanged.
Tuple literals can use (), a trailing comma for one item, or comma-separated items such as a, b, c. Parentheses are optional except for the empty tuple or where syntax requires disambiguation.
Follow-up: Why does f((a, b)) differ from f(a, b)? The former passes one tuple argument, while the latter passes two arguments.
listrangeenumeratesetdictfrozenset