Python Tuple Methods
Tuple operations support ordered immutable records. The two tuple-specific methods are count and index, alongside ordinary sequence access and built-in iteration tools.
Understand the idea
count reports occurrences, while index returns the first equal position or raises ValueError. A tuple can contain nested mutable values, so container immutability does not guarantee a deeply immutable record. Hashability requires every element to be hashable too. Use a named structure when many positional fields become difficult to remember.
Where to try it
Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.
A worked example
levels = (2, 4, 2)
print(levels.count(2), levels.index(4))
print(tuple(reversed(levels)))
Read the result
The counts and index are 2 and 1. The reversed tuple is (2, 4, 2).
Try one change
Use a non-symmetric tuple to verify reversal rather than relying only on an input that looks identical backward.
Quick lookup
| Operation | Meaning and limit |
|---|---|
count | Number of equal occurrences, including zero when absent. |
index | First equal position; raise ValueError when missing. |
tuple(iterable) | Consume an iterable into a tuple; not deep copying. |
len / in / slicing | Read length, membership, or a new selected tuple. |
Packing and unpacking | Use commas to pack and a documented shape to unpack. |