Python Tuple Methods

About This Index
This page is the Python Tuple Methods index for the tutorial track. A tuple is an ordered group of values that can never change, so it needs far fewer methods than a list: just count() and index(). Each guide has syntax, parameters, return value, three examples with output, and FAQs.
New to tuples? Start with the Tuple hub: creating tuples, the one-item comma trap, indexing, unpacking, and tuples vs lists.
Because a tuple is immutable (it cannot be changed after it is created), there is no append(), remove(), pop(), or sort(). Both methods only read the tuple and return an int.
How to Call a Tuple Method
Write the tuple, a dot, the method name, and the value in parentheses. Both methods hand back a number, so print it or store it in a variable:
codes = ("x", "y", "x", "z", "x")
print(codes.count("x")) # 3
print(codes.count("w")) # 0
print(codes.index("z")) # 3
print(codes.index("x", 1)) # 2
print(codes.index("x", -1)) # 4 count("w") returns 0 for a missing value, while index() would raise ValueError for it. The second argument of index() is where the search starts; -1 starts at the last item.
Python Tuple Methods Table
Every tuple method in this Python tutorial series (2 tutorials).
Related Tuple Features
These are not tuple methods: they are built-in functions and operators that work on tuples (and other sequences). You will use them next to count() and index() all the time:
| Code | What it does |
|---|---|
len(t) | Number of items in the tuple. |
x in t | True if some item is equal to x; it stops at the first match. x not in t is the opposite. |
t + other | New tuple with the items of both; other must be a tuple too, so write t + (5,) to add one item. |
t * n | New tuple with the items repeated n times, such as (0,) * 3. |
t[i], t[a:b] | Read one item by index, or slice out a new tuple from a up to, but not including, b. |
a, b = t | Unpacking: one name per item. a, *rest = t puts the leftovers in a list. |
tuple(iterable) | Build a tuple from a list, string, range, or any other iterable. |
sorted(t) | New sorted list; the tuple stays as it is. Wrap it as tuple(sorted(t)) for a tuple. |
min(t), max(t), sum(t) | Smallest item, largest item, and the total of numeric items. |
list(t) | Convert to a list when you really need to add, remove, or sort items in place. |
zip(a, b) | Pair up the items of two tuples as (a_item, b_item) tuples. |
enumerate(t) | Loop with (index, item) pairs: every position, not just the first match. |
t = (40, 10, 30, 20)
print(len(t), 30 in t) # 4 True
print(t + (50,), (0,) * 3) # (40, 10, 30, 20, 50) (0, 0, 0)
print(t[1:3], t[::-1]) # (10, 30) (20, 30, 10, 40)
a, b, *others = t
print(a, b, others) # 40 10 [30, 20]
print(sorted(t), min(t), max(t), sum(t)) # [10, 20, 30, 40] 10 40 100
print(tuple([1, 2]), list(t)) # (1, 2) [40, 10, 30, 20] Notice which results are tuples and which are lists: +, *, and slicing build new tuples, but sorted(t), list(t), and the starred name in a, b, *others = t give lists. The original t is the same at the end as at the start.
Frequently Asked Questions
Start with count()
Open the first tuple method guide: count how many times a value appears.
11 people found this page helpful
