Okay. You could see that the method we wrote didn't work. How can we correctly remove all negative numbers from the list? The secret is to create a copy of the list. Take a look:
sleep_durations = [268, -590, -326, 506]
for duration in sleep_durations[:]:
if duration < 0:
Instead of iterating over
sleep_durations, we created a temporary copy using
sleep_durations[:]. The syntax
[:] means "copy all elements from the list". When we use this, we iterate over a copy containing all the list elements in each iteration, but we remove items from the actual list. This gives us the correct algorithm.