Python Tuples Explained
A tuple is one of Python’s built-in collection types. It lets you store multiple values in a single object.
Tuples are useful when values belong together and should not change after they are created. In this guide, you’ll learn what tuples are, how to create them, how to access their items, and when to use them instead of lists.
What a tuple is
A tuple is an ordered collection of values.
This means:
- A tuple can hold more than one item
- The items stay in a specific order
- You can store different data types in the same tuple
- Each item keeps its position
One important rule is that tuples are immutable.
Immutable means you cannot change the items after the tuple is created. You cannot replace, add, or remove items directly.
person = ("Alice", 25, True)
print(person)
print(type(person))
Output:
('Alice', 25, True)
<class 'tuple'>
In this example:
"Alice"is a string25is an integerTrueis a boolean
All three values are stored in one tuple.
If you are new to this idea, see also what a tuple means in Python.
How to create a tuple
You create a tuple using comma-separated values, usually inside parentheses.
Empty tuple
empty_tuple = ()
print(empty_tuple)
print(type(empty_tuple))
Output:
()
<class 'tuple'>
Tuple with multiple items
numbers = (1, 2, 3)
print(numbers)
Output:
(1, 2, 3)
Single-item tuple
A single-item tuple must include a trailing comma.
value = (5,)
print(value)
print(type(value))
Output:
(5,)
<class 'tuple'>
Without the comma, Python does not create a tuple.
not_a_tuple = (5)
print(not_a_tuple)
print(type(not_a_tuple))
Output:
5
<class 'int'>
If you want more examples, see creating a tuple in Python.
How tuples differ from lists
Tuples and lists are similar in some ways, but they are not the same.
Main differences
- Lists use square brackets:
[] - Tuples usually use parentheses:
() - Lists are mutable
- Tuples are immutable
- Both are ordered
- Both support indexing and slicing
Example:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_list[0] = 99
print(my_list)
# my_tuple[0] = 99 # This would cause an error
print(my_tuple)
Output:
[99, 2, 3]
(1, 2, 3)
Use a tuple when the data should stay fixed. Use a list when you need to change the data.
For a full comparison, read Python lists explained or when to use lists vs tuples vs sets vs dictionaries.
Accessing tuple items
You use an index to get one item from a tuple.
Index positions start at 0.
colors = ("red", "green", "blue")
print(colors[0])
print(colors[1])
print(colors[2])
Output:
red
green
blue
Negative indexes
Negative indexes count from the end.
colors = ("red", "green", "blue")
print(colors[-1])
print(colors[-2])
Output:
blue
green
Invalid indexes
If you try to access a position that does not exist, Python raises an IndexError.
colors = ("red", "green", "blue")
print(colors[5])
Output:
IndexError: tuple index out of range
If you get that error, see how to fix tuple index out of range or read more about tuple indexing in Python.
Tuple slicing
Slicing lets you get part of a tuple.
The basic pattern is:
my_tuple[start:end]
Slicing returns a new tuple.
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
print(numbers[:3])
print(numbers[2:])
Output:
(20, 30, 40)
(10, 20, 30)
(30, 40, 50)
How it works:
numbers[1:4]gets items from index1up to but not including4numbers[:3]starts from the beginningnumbers[2:]goes to the end
You can learn more on the tuple slicing explained page.
Tuple packing and unpacking
Packing
Packing means storing multiple values in one tuple.
point = 10, 20
print(point)
print(type(point))
Output:
(10, 20)
<class 'tuple'>
Notice that parentheses are not always required. The commas are what matter most.
Unpacking
Unpacking means assigning tuple items to separate variables.
point = (10, 20)
x, y = point
print(x)
print(y)
Output:
10
20
The number of variables must match the number of tuple items.
This causes an error:
point = (10, 20, 30)
x, y = point
Output:
ValueError: too many values to unpack (expected 2)
This also causes an error:
point = (10,)
x, y = point
Output:
ValueError: not enough values to unpack (expected 2, got 1)
If that happens, see:
Useful tuple operations
Python gives you a few simple tools for working with tuples.
len()
Use len() to get the number of items.
numbers = (10, 20, 30)
print(len(numbers))
Output:
3
in
Use in to check whether a value exists.
numbers = (10, 20, 30)
print(20 in numbers)
print(99 in numbers)
Output:
True
False
count()
Use count() to count how many times a value appears.
values = (1, 2, 2, 3, 2)
print(values.count(2))
Output:
3
index()
Use index() to find the position of a value.
values = ("a", "b", "c")
print(values.index("b"))
Output:
1
Related references:
When to use tuples
Use tuples when you have a fixed group of related values.
Common examples:
- Coordinates like
(10, 20) - RGB colors like
(255, 0, 0) - Settings that should stay unchanged
- Multiple values returned from a function
Example:
def get_dimensions():
return (1920, 1080)
width, height = get_dimensions()
print(width)
print(height)
Output:
1920
1080
A good rule is:
- Use a tuple when the values should stay unchanged
- Use a list when you need to add, remove, or edit items
If you want to understand this idea better, see mutability in Python: mutable vs immutable types.
Common mistakes
Beginners often run into the same tuple problems.
Forgetting the comma in a single-item tuple
This does not create a tuple:
value = (5)
print(type(value))
This does:
value = (5,)
print(type(value))
Trying to change a tuple item
This causes an error because tuples are immutable:
numbers = (1, 2, 3)
numbers[0] = 99
Mixing up tuple syntax with list syntax
Lists use square brackets:
my_list = [1, 2, 3]
Tuples usually use parentheses:
my_tuple = (1, 2, 3)
Using the wrong number of variables during unpacking
Make sure the number of variables matches the number of items.
point = (10, 20)
x, y = point
Accessing an index that does not exist
Check the tuple length before using a position if you are not sure.
Useful debugging commands:
print(my_tuple)
print(type(my_tuple))
print(len(my_tuple))
print(my_tuple[0])
These help you confirm:
- what the tuple contains
- whether the object is really a tuple
- how many items it has
- what is stored at the first position
FAQ
Are tuples faster than lists?
They can be slightly faster for some operations, but beginners should mainly think about whether the data should be changeable or fixed.
Can a tuple contain different data types?
Yes. A tuple can contain strings, numbers, booleans, and other objects in the same tuple.
Can you change a tuple after creating it?
No. You cannot directly add, remove, or replace items in a tuple.
Why does a one-item tuple need a comma?
Without the comma, Python treats the value as a normal expression, not as a tuple.
When should I use a tuple instead of a list?
Use a tuple when the values belong together and should not change.