Python Tuple Indexing Explained
Tuple indexing is how you access one item in a tuple by its position.
Use this when you want to read a specific value from a tuple. On this page, the focus is on:
- positive indexes
- negative indexes
- common indexing mistakes
- what happens when an index does not exist
Quick answer
Use square brackets with a number after the tuple name.
colors = ("red", "green", "blue")
print(colors[0]) # red
print(colors[1]) # green
print(colors[-1]) # blue
- Index
0is the first item. - Negative indexes count from the end.
If you need help creating tuples first, see how to create a tuple in Python.
What tuple indexing means
An index is the position of an item in a tuple.
Tuple indexing works because tuples are ordered. That means each item has a fixed position.
Important points:
- An index tells Python which item you want
- Python starts counting at
0, not1 - Indexing reads one item at a time
- The tuple itself does not change when you read from it
Example:
numbers = (10, 20, 30)
print(numbers[0])
print(numbers[1])
print(numbers[2])
Output:
10
20
30
If you want a broader introduction, see Python tuples explained.
How positive indexes work
Positive indexes count from the beginning of the tuple.
0= first item1= second item2= third item
Pattern:
tuple_name[index]
Example:
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # cherry
This is the most common way to access tuple items when you know the position from the start.
How negative indexes work
Negative indexes count from the end of the tuple.
-1= last item-2= second-to-last item-3= third-to-last item
Example:
fruits = ("apple", "banana", "cherry")
print(fruits[-1]) # cherry
print(fruits[-2]) # banana
print(fruits[-3]) # apple
Negative indexing is useful when you need the last item but do not want to calculate its position manually.
This is especially helpful when the tuple size may change.
Example with a simple tuple
Here is a simple example that reads the first, middle, and last item from a tuple.
names = ("Ana", "Ben", "Cara")
print(names[0]) # first item
print(names[1]) # middle item
print(names[-1]) # last item
Output:
Ana
Ben
Cara
Notice that indexing only reads values. It does not modify the tuple.
IndexError when the position does not exist
If you try to access an index that is outside the tuple length, Python raises an error.
Example:
letters = ("a", "b", "c")
print(letters[5])
Output:
Traceback (most recent call last):
...
IndexError: tuple index out of range
This happens because the tuple has only 3 items, so valid positive indexes are:
012
One simple way to avoid this is to check the length first with len() on a tuple.
letters = ("a", "b", "c")
print(len(letters)) # 3
if len(letters) > 2:
print(letters[2])
If you are dealing with this error now, see how to fix IndexError: tuple index out of range.
Tuple indexing vs tuple slicing
Indexing and slicing are related, but they are not the same.
- Indexing gets one item
- Slicing gets a range of items
Indexing example:
numbers = (10, 20, 30, 40)
print(numbers[1])
Output:
20
Slicing example:
numbers = (10, 20, 30, 40)
print(numbers[1:3])
Output:
(20, 30)
This page focuses on getting one item by position. If you want to get multiple items, see Python tuple slicing explained.
Important beginner note about tuples
You can read tuple items by index, but you cannot change them by index.
Tuples are immutable. That means their values cannot be changed after creation.
This works:
colors = ("red", "green", "blue")
print(colors[1])
But this does not work:
colors = ("red", "green", "blue")
colors[1] = "yellow"
Python will raise an error because tuple items cannot be reassigned.
If you need a collection where items can be changed, use a list instead.
Common mistakes
Here are some common beginner mistakes with tuple indexing:
- Starting at index
1instead of0 - Using an index that is too large
- Confusing negative indexes with subtraction
- Trying to assign a new value to
tuple[index]
Useful checks while debugging:
print(my_tuple)
print(len(my_tuple))
print(my_tuple[0])
print(my_tuple[-1])
print(type(my_tuple))
These can help you confirm:
- what the tuple contains
- how many items it has
- what the first and last items are
- whether the variable is really a tuple
FAQ
Does tuple indexing start at 0?
Yes. The first item is at index 0.
How do I get the last item in a tuple?
Use -1 as the index:
my_tuple[-1]
What happens if I use an invalid index?
Python raises:
IndexError: tuple index out of range
Can I change a tuple item using its index?
No. Tuples are immutable, so indexed assignment is not allowed.