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.
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
Memory formula: Find → Count → Loop → Merge
1. index() — find the first position
Syntax: listobj.index(value)
index() tells you where a value appears first.
lst = [10, "RS", 45.67, "Python", True]
print(lst.index("Python")) # 3If value does not exist
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.
lst = [10, 20, 30, 10, 30, 20, 40, 10]
print(lst.count(10)) # 3
print(lst.count(100)) # 0Character frequency
word = "MISSISSIPPI"
letters = list(word)
print(letters.count("I")) # 4
print(letters.count("S")) # 4
count()never throws an error. Missing values simply return0.
index() vs count()
Finds the first position
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:
for value in lst:
print(value)With enumerate() you get both:
for index, value in enumerate(lst):
print(index, value)lst = [10, 20, 30, 10]
for i, v in enumerate(lst):
print(i, "→", v)Output:
0 → 10
1 → 20
2 → 30
3 → 10Finding ALL positions of a value
This is a very common interview question.
lst = [10, 20, 30, 10, 40, 10]
for i, v in enumerate(lst):
if v == 10:
print(i)
# 0, 3, 5index() |
enumerate() |
|---|---|
| Only first match | Every match |
| Returns single integer | Loop through all |
Character frequency with enumerate()
Works on strings too — strings are iterable.
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.
enumerate(10) # TypeError: 'int' object is not iterable4. extend() — merge lists
Syntax: listobj1.extend(listobj2)
append() adds one object. extend() adds every element from another iterable.
Visual difference
[1,2].append([3,4]) → [1, 2, [3, 4]]
[1,2].extend([3,4]) → [1, 2, 3, 4]
lst1 = [10, 20, 30]
lst2 = ["A", "B"]
lst1.extend(lst2)
print(lst1) # [10, 20, 30, 'A', 'B']
print(lst2) # ['A', 'B'] — unchangedNotice: lst1 changes; lst2 stays the same. extend() takes exactly one iterable argument.
extend() vs +
Mutates existing list
Creates another list
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:
lst1 = [10, 20]
lst2 = ["A", "B"]
lst3 = [1.2, 2.3]
lst1.extend(lst2)
lst1.extend(lst3)
# Or: lst1 = lst1 + lst2 + lst3 # new listMini project — shopping cart merge
Imagine two family members add items separately.
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
Output:
2
2You 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:
- Find the first index of
"Python"in a mixed list. - Count how many times
20appears. - Print all indices where value is
10(useenumerate()). - Merge three lists using
extend(). - 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…