You have a list [1, 2, 3, 4, 5] and write a loop to remove all even numbers by calling remove() on each even element directly during the iteration. What is the most likely outcome?
AAll even numbers are correctly removed, leaving [1, 3, 5]
BThe loop crashes immediately with an index error on the first removal
CSome even numbers are silently skipped, producing incorrect output
DThe list is unchanged because remove() is not allowed inside loops
When you remove an element at index i, all subsequent elements shift left by one. The loop then advances to index i+1, which now points to what was previously at i+2 — silently skipping the element that just moved into position i. In this case, removing 2 (index 1) shifts 3 into index 1, but the loop advances to index 2 (now 4), skipping 3. The safe alternatives are: build a new list with desired elements, collect indices after the loop, or iterate in reverse.
Question 2 Multiple Choice
You need to process every element in a list and also need to know the index of each element. Which approach is most idiomatic in Python?
AA while loop with a manually managed counter variable
BA plain for-each loop (for item in items) and track the index separately
Cenumerate() to get both index and value in each iteration
DAn index-based loop using range(len(items)) and items[i]
enumerate() is the idiomatic Python solution: 'for i, item in enumerate(items)' gives both the index and value cleanly. An index-based range(len(items)) loop works but is more verbose and reintroduces off-by-one risk. A while loop with a manual counter is even more error-prone. The plain for-each loop alone cannot provide the index without a workaround.
Question 3 True / False
A for-each loop (e.g., 'for item in items') eliminates off-by-one errors because you never manage an index variable manually.
TTrue
FFalse
Answer: True
Off-by-one errors — starting at 1 instead of 0, writing i <= len(items) instead of i < len(items) — arise specifically from manually managing the index variable. For-each loops hand control of iteration to the language runtime, so you cannot accidentally misconfigure the bounds. The tradeoff is that you lose direct index access and the ability to skip, reverse, or access neighboring elements.
Question 4 True / False
It is safe to remove elements from a list while iterating over it with an index-based for loop, because the index variable generally tracks your current position correctly.
TTrue
FFalse
Answer: False
The index variable correctly tracks your position in the current array — but the current array changes when you remove an element. Removing the element at index i shifts all subsequent elements left by one. When the loop increments to i+1, it now points to what was previously at i+2, silently skipping one element. The index is correct relative to the modified array, but the modified array is no longer the same structure the loop was initialized against.
Question 5 Short Answer
Why is it dangerous to modify a collection while iterating over it, and what is the standard safe alternative?
Think about your answer, then reveal below.
Model answer: When elements are removed during iteration, subsequent elements shift in memory, causing the iterator to skip positions. When elements are added, the loop may visit them unexpectedly or run indefinitely. The collection's structure changes beneath an iterator that assumed it was stable. Safe alternatives are: (1) build a new collection with the desired elements using a comprehension or filter, (2) collect elements or indices to modify and apply changes after the loop completes, or (3) iterate in reverse for removal, so shifts don't affect unvisited (earlier) positions.
This applies to both index-based and for-each loops, though the failure modes differ. For-each loops in many languages (Java) raise an explicit ConcurrentModificationException, making the bug visible. Index-based loops silently skip elements, making the bug harder to detect. The silent failure mode of index loops is arguably more dangerous because it produces wrong output with no error.