Delete Starting Even Numbers

Hello,
It seems like I got into a problem and I cannot understand where my problem is and why it’s happening.
The question:
Write a function called delete_starting_evens() that has a parameter named lst .

The function should remove elements from the front of lst until the front of the list is not even. The function should then return lst .

For example if lst started as [4, 8, 10, 11, 12, 15] , then delete_starting_evens(lst) should return [11, 12, 15] .

Make sure your function works even if every element in the list is even!

My answer:
def delete_starting_evens(lst):

for num in lst:

if num % 2 == 0 and len(lst) > 0:

  lst.remove(num)

elif num % 2 != 0:

  break

return lst

The program just skips on 8 and doesn’t break when it meets the number 11.
Thanks for your help :smile:

Be careful about modifying a list you are iterating through, it often leads to unexpected behaviour. If you’re not sure about the values as you’re iterating through try using print to output them at sensible points.

1 Like