BugLab007 / 020up to 100
listsiterationmutationhard

Vanishing Elements

The function remove_negatives(numbers) should return a list with all negative numbers removed.

It seems to work for some inputs, but with others some negatives survive even though the code clearly checks every element.

This is a particularly nasty bug — the code looks completely correct. The logic is right. Yet the output is wrong.

# your fix
+ must use for
hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def remove_negatives(numbers):
for n in numbers:
if n < 0:
numbers.remove(n)
return numbers
>>> remove_negatives([-1, -2, 3])