Exercise 7: Remove All Occurrences of a Value — Possible Solution ==================================================================== numbers = [1, 2, 3, 2, 4, 2, 5] index = 0 while index < len(numbers): if numbers[index] == 2: del numbers[index] # Do NOT increment index here — the next element has just # slid down into this same position. else: index += 1 print(numbers) Output: [1, 3, 4, 5] WHY THIS WORKS AS AN ANSWER ------------------------------ This is the single trickiest gotcha in the whole exercise set. When del numbers[index] removes an element, every element after it shifts one position to the LEFT to fill the gap — so the element that was at index + 1 is now sitting at index. If the loop naively did index += 1 after every deletion (the same as it does for a non-match), it would skip over that shifted element entirely, checking it never at all. The fix is to only advance the index when nothing was deleted. When a match is found and removed, index deliberately stays put, so the very next iteration re-checks that same position — which now holds a different value. This is exactly why the increment in a while loop can't be written on "autopilot" at the bottom of the loop the way it often can in a simple counting loop: sometimes the correct behaviour is to NOT advance, and getting that wrong here doesn't cause an infinite loop, it causes a silent, wrong answer (a leftover 2 in the result) — arguably a worse bug to have, since nothing crashes or hangs to reveal it.