Common Lists Mistakes
You've covered a lot of ground with lists. Here are the mistakes that show up most often — and how to fix them.
1. Capturing None from a mutating method
squad = ["Raven", "Wolf"]
squad = squad.append("Ghost")
print(squad)
Output → None
append(), sort(), reverse(), clear() — they all modify the list in place and return None. If you assign the result, you overwrite your list with nothing.
squad = ["Raven", "Wolf"]
squad.append("Ghost")
print(squad)
Output → ['Raven', 'Wolf', 'Ghost']
Call the method. Don't assign it.
2. IndexError — going out of bounds
squad = ["Raven", "Wolf", "Ghost"]
print(squad[3])
Output → IndexError: list index out of range
A list with 3 elements has indexes 0, 1, 2. Index 3 doesn't exist.
print(squad[2]) # last element — index is len(squad) - 1
print(squad[-1]) # or just use -1
When in doubt, check len() before indexing.
3. remove() on a value that isn't there
squad = ["Raven", "Wolf", "Ghost"]
squad.remove("Fox")
Output → ValueError: list.remove(x): x not in list
Always check with in first:
if "Fox" in squad:
squad.remove("Fox")
4. Confusing append() and extend()
squad = ["Raven", "Wolf"]
squad.append(["Ghost", "Viper"])
print(squad)
Output → ['Raven', 'Wolf', ['Ghost', 'Viper']]
append() adds the list as a single element — a nested list you didn't want.
squad = ["Raven", "Wolf"]
squad.extend(["Ghost", "Viper"])
print(squad)
Output → ['Raven', 'Wolf', 'Ghost', 'Viper']
Use extend() when you want to add multiple elements from another list.
5. Modifying a list while looping over it
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
numbers.remove(num)
print(numbers)
Output → [1, 3, 5] — sometimes. Other times elements get skipped silently.
When you remove an element during a loop, Python shifts the remaining elements and the loop loses its place.
Loop over a copy instead, or build a new list:
numbers = [1, 2, 3, 4, 5]
odd = []
for num in numbers:
if num % 2 != 0:
odd.append(num)
print(odd)
Output → [1, 3, 5]
6. Assignment is not a copy
original = ["Raven", "Wolf", "Ghost"]
backup = original
backup.append("Viper")
print(original)
Output → ['Raven', 'Wolf', 'Ghost', 'Viper']
backup = original doesn't create a new list. Both variables point to the same one. Any change through either variable affects both.
backup = original.copy()
backup.append("Viper")
print(original) # ['Raven', 'Wolf', 'Ghost']
print(backup) # ['Raven', 'Wolf', 'Ghost', 'Viper']
Use copy() when you need an independent list.
7. Using index() without checking first
squad = ["Raven", "Wolf", "Ghost"]
print(squad.index("Fox"))
Output → ValueError: 'Fox' is not in list
if "Fox" in squad:
print(squad.index("Fox"))
else:
print("Not found.")
count() is safer for existence checks — it returns 0 instead of raising an error.
8. Forgetting that slice stop is exclusive
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad[0:3])
Output → ['Raven', 'Wolf', 'Ghost']
You asked for indexes 0 to 3 — but index 3 is not included. You get 0, 1, 2.
If you want the first 3 elements: squad[:3]. If you want up to and including index 3: squad[:4].
What you should take away
- Mutating methods return
None— don't assign them - Check bounds before indexing, check
inbefore removing - Use
extend()to add multiple elements, notappend() - Never modify a list while looping over it
- Use
copy()when you need an independent list - Slice stop is exclusive —
list[0:3]gives you indexes 0, 1, 2