A tuple is an ordered collection, like a list, but you can’t change it after you create it. Tuples are used for fixed data such as coordinates, dates and values returned from a function. This lesson shows how to create tuples, read them, unpack them, and decide when to use a tuple instead of a list.
Try every example in our online Python compiler.
Create a tuple
A tuple is written with round brackets ().
fruits = ('apple', 'banana', 'cherry')
print(fruits)
print(type(fruits))Output:
('apple', 'banana', 'cherry') <class 'tuple'>
A tuple with one item
A tuple with a single item needs a comma after the item. Without the comma, Python treats the value as a normal number or string.
print(type((5,)))
print(type((5)))
print(type(tuple([1, 2])))Output:
<class 'tuple'> <class 'int'> <class 'tuple'>
Access items
Indexing and slicing work exactly as they do for lists. The first index is 0, and negative indexes count from the end.
fruits = ('apple', 'banana', 'cherry')
print(fruits[0])
print(fruits[-1])
print(fruits[1:])
print(len(fruits))Output:
apple cherry ('banana', 'cherry') 3
Tuples cannot be changed
Tuples are immutable. You cannot change, add or remove an item.
fruits = ('apple', 'banana', 'cherry')
try:
fruits[0] = 'mango'
except TypeError as e:
print('TypeError:', e)Output:
TypeError: 'tuple' object does not support item assignmentIf you need to change a tuple, convert it to a list, make the change, and convert it back:
fruits = ('apple', 'banana', 'cherry')
items = list(fruits)
items.append('date')
print(items)
print(tuple(items))Output:
['apple', 'banana', 'cherry', 'date'] ('apple', 'banana', 'cherry', 'date')
Unpack a tuple
Unpacking assigns the items of a tuple to separate variables in one step.
fruits = ('apple', 'banana', 'cherry')
a, b, c = fruits
print(a, b, c)
first, *rest = (1, 2, 3, 4)
print(first, rest)Output:
apple banana cherry
1 [2, 3, 4]The number of variables must match the number of items, unless you use * to collect the extra items into a list.
Tuple methods
A tuple has only two methods, because it cannot be changed.
nums = (1, 2, 2, 3, 2)
print(nums.count(2))
print(nums.index(3))Output:
3
3count(x) tells you how many times x appears, and index(x) gives the index of the first x.
Join, repeat and search
nums = (1, 2, 2, 3, 2)
print((1, 2) + (3,))
print((0,) * 3)
print(2 in nums)Output:
(1, 2, 3) (0, 0, 0) True
Loop through a tuple
fruits = ('apple', 'banana', 'cherry')
for item in fruits:
print(item)Output:
apple banana cherry
Return many values from a function
A function can return several values as a tuple, and you can unpack them into variables.
def min_max(values):
return min(values), max(values)
result = min_max([4, 9, 1])
print(result)
low, high = min_max([4, 9, 1])
print(low, high)Output:
(1, 9) 1 9
Swap two variables
Tuple unpacking gives the shortest way to swap two values.
x, y = 1, 2
x, y = y, x
print(x, y)Output:
2 1
Nested tuples and tuples as dictionary keys
nested = ((1, 2), (3, 4))
print(nested[1][0])
points = {(1, 2): 'point'}
print(points[(1, 2)])Output:
3
pointBecause a tuple cannot be changed, it can be used as a dictionary key. A list cannot.
Tuple vs list
| List | Tuple |
|---|---|---|
Brackets | [ ] | ( ) |
Can be changed | Yes | No |
Methods | Many | Only count() and index() |
Can be a dictionary key | No | Yes |
Best for | Data that changes | Fixed data |
Tuples are also a little smaller and faster than lists, but the main reason to use one is to show that the data should not change.
Common mistakes
- Forgetting the comma in a one-item tuple. Write (5,), not (5).
- Trying to change a tuple. Convert it to a list first, or create a new tuple.
- Unpacking into the wrong number of variables. a, b = (1, 2, 3) raises a ValueError.
Practice questions
- Create a tuple of your five favourite colours and print the last two.
- Count how many times 5 appears in (5, 1, 5, 2, 5).
- Write a function that returns the sum and the average of a list as a tuple.
- Convert the tuple (1, 2, 3) to a list, append 4, and convert it back.
Summary
- A tuple is an ordered collection written with ( ), and it cannot be changed.
- Use indexing, slicing and unpacking to read it.
- A one-item tuple needs a trailing comma.
- Use a tuple for fixed data, and a list for data that changes.
