Back
+10 XP
Concept

Searching & Counting: `.index()`, `.count()`

Check existence with `in`. Find the first position of an item with `.index(val)`. Count how many times it shows up with `.count(val)`.

python
letters = ["a", "b", "a", "c"]
print("b" in letters)      # True
print(letters.index("b"))  # 1
print(letters.count("a"))  # 2

# 71. Searching & Counting: `.index()`, `.count()` Check existence with `in`. Find the first position of an item with `.index(val)`. Count how many times it shows up with `.count(val)`: ```python letters = ["a", "b", "a", "c"] print("b" in letters) # True print(letters.index("b")) # 1 print(letters.count("a")) # 2 ``` Gotcha: Never use `.index()` blindly without checking `if val in letters:` first, or Python will panic and crash when the item is missing.

Checkpoint

What does `letters.count('z')` return if `'z'` is not in the list?

Practice

Count how many times `7` appears in `lucky = [7, 3, 7, 7, 2]`. Print the count.

Terminal
01
Tap code blocks below to assemble
Available Blocks