In Python, tuple(iterable) creates a tuple, an ordered sequence that is immutable: you cannot add, remove, or replace its items after it is created. tuple([1, 2]) is (1, 2) and tuple() is the empty tuple ().
Because they cannot change, tuples of hashable items are hashable and can be dict keys or set members. They are also what Python uses to return several values from a function and to unpack values on the left of =.
tuple is a built-in type (a class) that you call like a function: each call creates a new tuple object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax
Pass any iterable, or write a literal with commas.
python
items = tuple(iterable)
point = (3, 4)
single = (5,) # the comma makes it a tuple
Call
What it does
tuple([1, 2, 3])
Returns (1, 2, 3).
tuple("hi")
Returns ('h', 'i').
tuple()
Returns ().
(5,)
A one-item tuple; (5) is just the int 5.
Tuples have only two methods, count() and index(), because they cannot be modified.
Library declarations
python
tuple(iterable=(), /)
# class tuple: an immutable built-in type
Parameters
Parameter
Description
iterable
Optional. Any iterable. Default: empty.
Return Value
Return Value
Description
tuple
A tuple with the iterable's items (an existing tuple may be returned as is).
Python Examples
Example 1 — Create Tuples from Iterables
Convert a list, a string, and a range, and check a one-item tuple.
Implicit tuple.return a, b returns one tuple holding both values.
Unpacking.low, high = ... splits the tuple into two names.
Shallow immutability. The tuple still holds the same list, but that list can grow, so 77 appears.
Tuple methods.count() and index() search the items; the list is at index 1.
Frequently Asked Questions
A list is mutable and a tuple is immutable. Tuples are hashable when their items are, so they can be dict keys, and they signal data that should not change.
Add a trailing comma: (5,) or 5,. Without the comma, (5) is just the number 5.
Call tuple(my_list).
No items can be added, removed, or replaced. Build a new tuple instead, for example t + (4,).
It contains a mutable item such as a list or dict. Every item must be hashable for the tuple to be hashable.