tuple()
Builds a tuple, an ordered sequence that cannot be changed after it is made, from a list, a string or anything else you can loop over.
tuple(iterable=())tuple() turns any iterable into a tuple: a sequence like a list, written with round brackets, that cannot be changed once it exists. tuple([1, 2, 3]) gives (1, 2, 3).
Use a tuple for a fixed group of values that belong together: a point (x, y), a date (2026, 3, 14), an RGB colour. Because tuples cannot change, they can be dictionary keys and set members, which lists cannot.
Tuples are made by commas more than by brackets. point = 3, 4 is already a tuple. That is why a one-item tuple needs a trailing comma: (5,) is a tuple, but (5) is just the number 5 in brackets.
Parameters
| Parameter | What it is |
|---|---|
iterableoptional, default () | Anything you can loop over. With nothing, you get the empty tuple (). |
Return value
A new tuple with the items in order. If you pass a tuple, you may get that same tuple back, since there is no need to copy something that cannot change.
Examples
1Make tuples from other things
print(tuple([1, 2, 3]))
print(tuple("abc"))
print(tuple(range(3)))
print(tuple())(1, 2, 3)
('a', 'b', 'c')
(0, 1, 2)
()2Tuples as dictionary keys
seats = {(1, "A"): "Lena", (1, "B"): "Omar", (2, "A"): "Yuki"}
print(seats[(1, "B")])
row, letter = 2, "A"
print(seats[(row, letter)])Omar Yuki
A list could not be used here: dictionary keys must not be changeable.
3Return several values from a function
def min_max(numbers):
return min(numbers), max(numbers)
result = min_max([14, 3, 27, 9])
print(result)
low, high = result
print(f"From {low} to {high}")(3, 27) From 3 to 27
return a, b returns one tuple, which you can unpack into two names.
4Freeze a list so nobody changes it
weekdays = tuple(["Mon", "Tue", "Wed", "Thu", "Fri"])
print(len(weekdays), weekdays[-1])
print("Sat" in weekdays)
print(weekdays.index("Wed"))5 Fri False 2
Tuples support reading: indexing, slicing, in, len(), count() and index().
Common mistakes
Forgetting the comma in a one-item tuple
Brackets alone only group. The comma is what makes a tuple.
a = (5)
b = (5,)
print(type(a).__name__, type(b).__name__)
print(b)int tuple (5,)
Trying to change an item
Tuples cannot be changed. Build a new tuple, or turn it into a list, change that, and convert back.
color = (255, 128, 0)
try:
color[1] = 64
except TypeError:
print("TypeError: tuples cannot be changed")
parts = list(color)
parts[1] = 64
color = tuple(parts)
print(color)TypeError: tuples cannot be changed (255, 64, 0)
Expecting tuple("word") to hold the word
Like list(), tuple() loops over a string and gives characters. For a tuple holding the whole word, write ("word",).
print(tuple("hi"))
print(("hi",))('h', 'i')
('hi',)Questions people ask
What is the difference between a list and a tuple in Python?
A list can change (add, remove, replace items); a tuple cannot. Use tuples for fixed records and as dictionary keys, lists for collections that grow or shrink.
How do I convert a tuple to a list?
list(my_tuple). And back with tuple(my_list).
Are tuples faster than lists?
Slightly, and they use a little less memory. The main reason to choose one is meaning: fixed record or changing collection.