Skip to content
Home / Fundamentals

Python Tuple Methods

In Python, a tuple is an immutable sequence type. This means that once you create a tuple, you cannot change the values it contains. This means that some methods which you might be familiar with the Python list are not available for the tuple. Here are some of the most common methods for working with tuples in Python:

count(value)

The count method returns the number of times a given value appears in the tuple. For example:

>>> my_tuple = (1, 2, 2, 3, 3, 3)
>>> my_tuple.count(2)
2
>>> my_tuple.count(3)
3
>>> my_tuple.count(4)
0

index(value)

The index method returns the index of the first occurrence of a given value in the tuple. For example:

>>> my_tuple = (1, 2, 2, 3, 3, 3)
>>> my_tuple.index(2)
1
>>> my_tuple.index(3)
3

If the value is not found in the tuple, the index method will raise a ValueError.

len(tuple)

The built-in len function returns the length of a tuple, i.e. the number of elements it contains. For example:

>>> my_tuple = (1, 2, 3)
>>> len(my_tuple)
3

max(tuple)

The built-in max function returns the maximum value in a tuple. For example:

>>> my_tuple = (1, 2, 3)
>>> max(my_tuple)
3

The max function only works for tuples that contain values that can be compared to each other, such as numbers or strings.

min(tuple)

The built-in min function returns the minimum value in a tuple. For example:

>>> my_tuple = (1, 2, 3)
>>> min(my_tuple)
1

Like the max function, the min function only works for tuples that contain values that can be compared to each other.

sum(tuple)

The built-in sum function returns the sum of all the values in a tuple. For example:

>>> my_tuple = (1, 2, 3)
>>> sum(my_tuple)
6

The sum function only works for tuples that contain numbers.