Python 3 reference · Built-in Functions

Python tuple() Function

Create a tuple: an ordered, immutable sequence. Tuples are hashable when their items are, so they can be dict keys.

Python 3 Since 3.0

Syntax

tuple(iterable=())

Parameters

ParameterDescription
iterable optional Items for the tuple.

Return value

A new tuple.

Examples

Create tuplesTry it yourself
print(tuple([1, 2, 3]))
print(tuple("abc"))
print((5,), type((5,)).__name__, type((5)).__name__)  # the comma makes a tuple

Output

(1, 2, 3)
('a', 'b', 'c')
(5,) tuple int
Unpacking and dictionary keysTry it yourself
point = (3, 4)
x, y = point          # unpacking
print(x, y)
distances = {(0, 0): "origin", (3, 4): "five away"}
print(distances[point])

Output

3 4
five away

Common mistakes

A one-item tuple needs a trailing comma: (5,). Without it, (5) is just the number 5 in parentheses.

Version history

VersionChange
Python 3.0Added