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
| Parameter | Description |
|---|---|
iterable optional |
Items for the tuple. |
Return value
A new tuple.
Examples
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 intpoint = (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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
list()· Create a list: an ordered, changeable sequence. Called with an iterable, it copies its items into a new list.frozenset()· Create an immutable set: unique items that can't be added or removed, so it is hashable and can be a dict key.frozendict()· Create an immutable, hashable dictionary (new in Python 3.15). It can be a dict key or set member and can't be changed.hash()· Return the hash value of an object: an integer used by dicts and sets to find keys quickly. Only immutable values are hashable.- Python Tuples · tutorial