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

Lesson 03

List Methods — index(), count(), enumerate(), extend()

Search Python lists with index() and count(), iterate with enumerate(), and merge lists using extend() or the + operator.

Intermediate25 min

What you will learn

Search · Count · Iterate · Merge lists

This lesson teaches how to find data without changing it and combine multiple lists. Unlike File 02 — mutation methods, these tools are mainly used for searching, reporting, and iteration.

Tool Role
index() First position of a value
count() How many times a value appears
enumerate() Index + value while looping
extend() Merge another list in place

Part of Collections lesson 3 in the module roadmap.

Learning roadmap

index count enumerate extend — search and merge flow

Memory formula: Find → Count → Loop → Merge

1. index() — find the first position

Syntax: listobj.index(value)

index() tells you where a value appears first.

index("Python") returns 3
python
lst = [10, "RS", 45.67, "Python", True]
print(lst.index("Python"))  # 3

If value does not exist

python
print(lst.index("Java"))
# ValueError: 'Java' is not in list
Situation Result
Value exists Returns first index
Value missing ValueError

2. count() — count frequency

Syntax: listobj.count(value)

Instead of finding where, count() finds how many.

count(10) = 3 highlighted cells
python
lst = [10, 20, 30, 10, 30, 20, 40, 10]
print(lst.count(10))   # 3
print(lst.count(100))  # 0

Character frequency

python
word = "MISSISSIPPI"
letters = list(word)
print(letters.count("I"))  # 4
print(letters.count("S"))  # 4

count() never throws an error. Missing values simply return 0.

index() vs count()

index finds first position of B

Finds the first position

count tallies all occurrences of A

Counts all occurrences

index() count()
Only first match Every match
Returns single integer Returns total count
ValueError if missing Returns 0 if missing

3. enumerate() — index + value together

Syntax: for index, value in enumerate(iterable):

Normally you only get the value:

python
for value in lst:
    print(value)

With enumerate() you get both:

python
for index, value in enumerate(lst):
    print(index, value)
enumerate pairs index column with value column
python
lst = [10, 20, 30, 10]
for i, v in enumerate(lst):
    print(i, "→", v)

Output:

text
0 → 10
1 → 20
2 → 30
3 → 10

Finding ALL positions of a value

This is a very common interview question.

python
lst = [10, 20, 30, 10, 40, 10]
for i, v in enumerate(lst):
    if v == 10:
        print(i)
# 0, 3, 5
index() enumerate()
Only first match Every match
Returns single integer Loop through all

Character frequency with enumerate()

Works on strings too — strings are iterable.

python
word = "MISSISSIPPI"
for i, ch in enumerate(word):
    if ch == "S":
        print(i, ch)
# 2 S, 3 S, 5 S, 6 S

enumerate() works on any iterable (list, tuple, string), but not integers.

python
enumerate(10)  # TypeError: 'int' object is not iterable

4. extend() — merge lists

Syntax: listobj1.extend(listobj2)

append() adds one object. extend() adds every element from another iterable.

Visual difference

append nests list as one element

[1,2].append([3,4]) → [1, 2, [3, 4]]

extend flattens elements into list

[1,2].extend([3,4]) → [1, 2, 3, 4]

python
lst1 = [10, 20, 30]
lst2 = ["A", "B"]
lst1.extend(lst2)
print(lst1)  # [10, 20, 30, 'A', 'B']
print(lst2)  # ['A', 'B'] — unchanged

Notice: lst1 changes; lst2 stays the same. extend() takes exactly one iterable argument.

extend() vs +

extend mutates same list object

Mutates existing list

plus operator creates new list

Creates another list

python
a = [1, 2]
b = [3, 4]
c = a + b
print(c)  # [1, 2, 3, 4]
# a is still [1, 2]
Feature extend() +
Creates new list? No Yes
Original changes? Yes No (unless you rebind)
Memory efficient? Better for in-place Slightly more memory

Chain three lists:

python
lst1 = [10, 20]
lst2 = ["A", "B"]
lst3 = [1.2, 2.3]
lst1.extend(lst2)
lst1.extend(lst3)
# Or: lst1 = lst1 + lst2 + lst3  # new list

Mini project — shopping cart merge

Imagine two family members add items separately.

python
cart_a = ["Apple", "Milk", "Bread"]
cart_b = ["Milk", "Eggs"]

cart_a.extend(cart_b)

print(cart_a.count("Milk"))
print(cart_a.index("Bread"))

Visual

Shopping cart extend flow — Milk appears twice

Output:

text
2
2

You used extend + count + index in one practical example.

Interview revision sheet

Method Purpose Returns
index(x) First position Integer
count(x) Total occurrences Integer
enumerate() Index + value pairs Iterator
extend(lst) Merge another iterable None

Practice questions

Test yourself before File 04 — sort, reverse, copy, del:

  1. Find the first index of "Python" in a mixed list.
  2. Count how many times 20 appears.
  3. Print all indices where value is 10 (use enumerate()).
  4. Merge three lists using extend().
  5. Merge three lists using the + operator.

Knowledge check

Q1: How do you find all indices of value 10?
Answer: Loop with enumerate(lst) and collect indices where v == 10.

Q2: lst1.extend(lst2, lst3) — valid?
Answer: No — TypeError. Call extend twice or use lst1 + lst2 + lst3.

Q3: index() vs count() when value is missing?
Answer: index raises ValueError; count returns 0.

Q4: Practice Q3 — why not use index() for every match?
Answer: index() only returns the first occurrence.

Loading playground…