Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate20 min

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

index count sorted tuple rebuild 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

First 10 highlighted at index 0
python
t = (10, 20, 30, 10, 40)

print(t.index(10))  # 0

Missing value

python
print(t.index(100))  # ValueError
Situation Result
Value exists First index
Value missing ValueError

3. count() — count occurrences

Three 10 values highlighted — count(10) = 3
python
t = (10, 20, 30, 10, 30, 20, 40, 10)

print(t.count(20))   # 2
print(t.count(100))  # 0

Difference

Method Missing value
index() Raises ValueError
count() Returns 0

4. What tuples cannot do

Because tuples never change size or order.

Not available on tuples — append remove insert pop sort reverse extend clear
python
t = (1, 2, 3)
t.append(4)  # AttributeError

Item assignment also fails:

python
t[0] = 10  # TypeError: 'tuple' object does not support item assignment

del on tuples

del t[i] is invalid — you cannot delete a single slot. Only del t removes the whole variable name.

python
t = (1, 2, 3)
del t
# print(t)  # NameError

5. sorted() — sort without changing the tuple

Important distinction:

  • sort() → list method (mutates in place)
  • sorted() → built-in function (returns new list)
sorted(t) returns new list — original tuple unchanged
python
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) — unchanged

Notice:

  • asc is a list
  • t is still a tuple

Compare with File 04 — list sort() where sort() mutates the same list.

6. Descending order

python
t = (10, 2, 13, -12, 56, 3)

print(sorted(t, reverse=True))

Output:

text
[56, 13, 10, 3, 2, -12]
Descending sorted result

7. Rebuild a sorted tuple

If you want the result to remain a tuple:

python
t = ("Zoom", "Rossum", "Anil")

new_tuple = tuple(sorted(t))

print(new_tuple)  # ('Anil', 'Rossum', 'Zoom')
tuple(sorted()) rebuilds sorted tuple

8. Aliasing tuples

Tuples are immutable, so aliasing is generally safe.

python
a = (10, 20)
b = a

print(a is b)  # True
a and b share one immutable tuple object

Both 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

python
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

Grade report — count index and sorted ranking

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:

python
t.sort()  # AttributeError — tuples have no sort()
sorted(t) # correct

Mistake 2 — thinking sorted() changes the tuple:

python
sorted(t)
print(t)  # original tuple unchanged

Correct: 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:

  1. Why does t.index(100) raise an error but t.count(100) returns 0?
  2. Can you call append() on a tuple? Why?
  3. What is the return type of sorted(t)?
  4. How do you create a sorted tuple instead of a list?
  5. 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…