W3docs

Python Tuples: An Overview

Learn how to access Python tuple elements using positive indexing, negative indexing, slicing, and nested access, with runnable examples.

This page explains how to read data out of a Python tuple. You will learn positive indexing, negative indexing, slicing, nested tuple access, the in operator, and the built-in len() function. You will also see when tuples are the right choice over lists.

What Is a Tuple?

A tuple is an ordered, immutable sequence of values. "Ordered" means every element has a fixed position you can address by index. "Immutable" means you cannot change, add, or remove elements after creation — that is what makes tuples useful when data integrity matters.

# Create a tuple with parentheses
colors = ("red", "green", "blue")

# The built-in tuple() function works too
digits = tuple([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

For everything you can do when first creating a tuple, see the Python Tuples chapter.

Accessing Elements by Index

Like lists, tuples use zero-based indexing: the first element is at index 0, the second at 1, and so on.

Index:    0        1         2
          |        |         |
colors = ("red", "green", "blue")

Syntax: tuple_name[index]

python— editable, runs on the server

Accessing an index that does not exist raises an IndexError:

colors = ("red", "green", "blue")
print(colors[5])  # IndexError: tuple index out of range

Negative Indexing

Python lets you count from the end of the tuple using negative numbers. Index -1 is the last element, -2 is second-to-last, and so on.

Index:    0        1         2
Neg idx: -3       -2        -1
          |        |         |
colors = ("red", "green", "blue")
python— editable, runs on the server

Negative indexing is especially convenient when you need the last item without calling len().

Slicing a Tuple

A slice extracts a sub-tuple. The syntax is tuple_name[start:stop] where start is inclusive and stop is exclusive.

fruits = ("apple", "banana", "cherry", "date", "elderberry")

print(fruits[1:3])   # ('banana', 'cherry')   — index 1 up to (not including) 3
print(fruits[:3])    # ('apple', 'banana', 'cherry')  — from beginning to index 3
print(fruits[2:])    # ('cherry', 'date', 'elderberry')  — from index 2 to end
print(fruits[:])     # entire tuple (useful for shallow copy)

You can add a step value as a third argument: tuple_name[start:stop:step].

numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

print(numbers[::2])   # (0, 2, 4, 6, 8)   — every second element
print(numbers[::-1])  # (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)  — reversed

A slice always returns a new tuple — the original is never modified.

Getting the Length with len()

len() returns the number of elements in a tuple.

fruits = ("apple", "banana", "cherry", "date", "elderberry")

print(len(fruits))  # 5

This is useful to guard against IndexError or to iterate with a range():

for i in range(len(fruits)):
    print(i, fruits[i])
# 0 apple
# 1 banana
# 2 cherry
# 3 date
# 4 elderberry

Checking Membership with in

The in operator tests whether a value exists in a tuple, returning True or False.

fruits = ("apple", "banana", "cherry")

print("banana" in fruits)   # True
print("mango" in fruits)    # False
print("mango" not in fruits)  # True

Use in instead of catching an exception when you simply need to know whether a value is present.

Accessing Nested Tuples

Tuples can contain other tuples. To reach an element inside a nested tuple, chain the index brackets.

matrix = ((1, 2), (3, 4), (5, 6))

print(matrix[0])       # (1, 2)
print(matrix[0][1])    # 2   — row 0, column 1
print(matrix[2][0])    # 5   — row 2, column 0

The same technique works for mixed nesting, such as a tuple of strings and numbers:

record = ("Alice", 30, ("Python", "SQL"))

print(record[0])       # Alice
print(record[2])       # ('Python', 'SQL')
print(record[2][0])    # Python

Tuple Operations

Although you cannot change a tuple, you can combine tuples to produce new ones.

Concatenating Tuples

Use + to join two or more tuples:

python— editable, runs on the server

Repeating Tuples

Use * to repeat a tuple a given number of times:

python— editable, runs on the server

Useful Tuple Methods

Tuples have two built-in methods that help you read data:

MethodWhat it returns
tuple.count(value)Number of times value appears in the tuple
tuple.index(value)Index of the first occurrence of value
colors = ("red", "green", "blue", "red", "red")

print(colors.count("red"))    # 3
print(colors.index("green"))  # 1

For a full reference, see the Tuple Methods chapter.

When to Use a Tuple Instead of a List

SituationTupleList
Data should not change (coordinates, RGB values, database rows)YesNo
Used as a dictionary keyYesNo — lists are not hashable
Slight memory / speed advantage for read-only dataYesNo
Need to append, remove, or sortNoYes

If you need to modify a tuple, see Update Tuples for the common workaround of converting to a list and back.

Practice

Practice
What are the ways to access items in a Python tuple?
What are the ways to access items in a Python tuple?
Was this page helpful?