How to Inspect a List
The problem...
You have a list. But you don't always know what's in it.
Is a specific value there? Where exactly is it? How many times does it appear? How long is the list?
You need tools to look inside — without modifying anything.
The idea!
Python gives you a set of methods and functions that read a list and report back.
They don't change the list. They just answer questions about it.
Checking if a value exists
Use in to check if a value is in the list. It returns True or False.
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print("Wolf" in squad)
print("Fox" in squad)
Output:
True
False
Use not in to check the opposite:
print("Fox" not in squad)
Output → True
How many elements
len() returns the number of elements in the list:
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(len(squad))
Output → 5
You already know len() from strings. It works the same way here.
Finding the position of a value
index() returns the index of the first matching element:
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad.index("Ghost"))
Output → 2
If the value appears more than once, you get the index of the first one.
If the value isn't in the list, Python raises a ValueError.
Check with in first if you're not sure:
if "Fox" in squad:
print(squad.index("Fox"))
else:
print("Not in the list.")
Counting occurrences
count() returns how many times a value appears:
scores = [85, 74, 85, 91, 85, 63]
print(scores.count(85))
Output → 3
If the value isn't in the list, count() returns 0 — no error.
Copying a list
copy() returns a new list with the same elements:
squad = ["Raven", "Wolf", "Ghost"]
backup = squad.copy()
backup.append("Viper")
print(squad)
print(backup)
Output:
['Raven', 'Wolf', 'Ghost']
['Raven', 'Wolf', 'Ghost', 'Viper']
The original isn't affected. Changes to backup stay in backup.
This matters because lists are mutable — without copy(), two variables pointing to the same list will both see every change.
Numbers in a list
For lists of numbers, Python gives you three more built-in functions:
scores = [85, 74, 91, 63, 98]
print(min(scores))
print(max(scores))
print(sum(scores))
Output:
63
98
411
What's really happening
None of these modify the list. They read it and return information.
in, len(), min(), max(), sum() are built-in functions — they work on the list from outside.
index(), count(), copy() are list methods — you call them on the list itself.
Heads up!
index()raisesValueErrorif the value isn't found — useinto check firstcount()returns0if the value isn't found — no errormin(),max(),sum()only work on lists of numbers- Assigning one list to another variable doesn't copy it — use
copy()
The mindset shift
Stop thinking: "I need to loop through the list to find things."
Start thinking: "Python already has tools for the most common questions — use them."
What you should understand now
- Use
inandnot into check if a value exists len()returns the number of elementsindex()returns the position of the first matchcount()returns how many times a value appearscopy()creates an independent copy of the listmin(),max(),sum()work on lists of numbers