Lesson 07
Tuple Methods — index(), count(), and sorted()
Use the only two tuple methods, understand what tuples cannot do, and sort tuple data with built-in sorted() without mutating the original.
What you will learn
Tuple Methods · Searching · Sorting · Immutability
Unlike lists, tuples are immutable. That is why they have only 2 methods: index() and count(). For sorting, Python provides the built-in sorted() function instead of a tuple method.
| Tool | Role |
|---|---|
index(x) |
Find first position of a value |
count(x) |
Count how many times a value appears |
sorted(t) |
Return a new sorted list |
tuple(sorted(t)) |
Rebuild a sorted tuple |
Memory formula: Find → Count → Sort (New List)
After File 06 — Tuple Deep Dive, you know how to create and protect tuple data. This lesson covers searching and sorting without mutation.
Learning roadmap
1. The only two tuple methods
Tuples are immutable, so Python gives them only 2 methods.
| Method | Purpose | Returns |
|---|---|---|
index(x) |
First position of value | Integer |
count(x) |
Total occurrences | Integer |
Remember: Everything else (
append,remove,sort...) belongs to lists, not tuples.
2. index() — find the first match
t = (10, 20, 30, 10, 40)
print(t.index(10)) # 0Missing value
print(t.index(100)) # ValueError| Situation | Result |
|---|---|
| Value exists | First index |
| Value missing | ValueError |
3. count() — count occurrences
t = (10, 20, 30, 10, 30, 20, 40, 10)
print(t.count(20)) # 2
print(t.count(100)) # 0Difference
| Method | Missing value |
|---|---|
index() |
Raises ValueError |
count() |
Returns 0 |
4. What tuples cannot do
Because tuples never change size or order.
t = (1, 2, 3)
t.append(4) # AttributeErrorItem assignment also fails:
t[0] = 10 # TypeError: 'tuple' object does not support item assignmentdel on tuples
del t[i] is invalid — you cannot delete a single slot. Only del t removes the whole variable name.
t = (1, 2, 3)
del t
# print(t) # NameError5. sorted() — sort without changing the tuple
Important distinction:
sort()→ list method (mutates in place)sorted()→ built-in function (returns new list)
t = (10, 2, 13, -12, 56, 3)
asc = sorted(t)
print(asc) # [-12, 2, 3, 10, 13, 56]
print(t) # (10, 2, 13, -12, 56, 3) — unchangedNotice:
ascis a listtis still a tuple
Compare with File 04 — list sort() where sort() mutates the same list.
6. Descending order
t = (10, 2, 13, -12, 56, 3)
print(sorted(t, reverse=True))Output:
[56, 13, 10, 3, 2, -12]7. Rebuild a sorted tuple
If you want the result to remain a tuple:
t = ("Zoom", "Rossum", "Anil")
new_tuple = tuple(sorted(t))
print(new_tuple) # ('Anil', 'Rossum', 'Zoom')8. Aliasing tuples
Tuples are immutable, so aliasing is generally safe.
a = (10, 20)
b = a
print(a is b) # TrueBoth variables point to the same tuple — that is okay because it cannot be modified. Contrast with list aliasing in File 04 — copy vs alias.
Mini project — student grades
grades = (88, 95, 72, 95)
print(grades.count(95)) # 2
print(grades.index(72)) # 2
ranking = tuple(sorted(grades, reverse=True))
print(ranking) # (95, 95, 88, 72)Visual result
This combines count + index + sorted + tuple in one practical example.
One-page revision sheet
| Task | Syntax |
|---|---|
| First position | t.index(x) |
| Count value | t.count(x) |
| Ascending sort | sorted(t) |
| Descending sort | sorted(t, reverse=True) |
| Sorted tuple | tuple(sorted(t)) |
| Delete tuple variable | del t |
Common interview mistakes
Mistake 1 — calling sort() on a tuple:
t.sort() # AttributeError — tuples have no sort()
sorted(t) # correctMistake 2 — thinking sorted() changes the tuple:
sorted(t)
print(t) # original tuple unchangedCorrect: sorted() returns a new list. Wrap with tuple() if you need a sorted tuple.
Practice questions
Test yourself before File 08 — Set Creation and Basics:
- Why does
t.index(100)raise an error butt.count(100)returns0? - Can you call
append()on a tuple? Why? - What is the return type of
sorted(t)? - How do you create a sorted tuple instead of a list?
- Why is aliasing (
b = a) generally safe for tuples?
Knowledge check
Q1: index(100) vs count(100) when value is missing?
Answer: index raises ValueError; count returns 0.
Q2: Can you call append() on a tuple?
Answer: No — tuples are immutable; no mutation methods exist.
Q3: Return type of sorted(t)?
Answer: A list, not a tuple.
Q4: Create a sorted tuple?
Answer: tuple(sorted(t)) or tuple(sorted(t, reverse=True)).
Q5: Why is tuple aliasing safe?
Answer: Both names refer to the same immutable object — neither can change the tuple's contents.
Loading playground…