What is wrong in my code? (Delete Starting Even Numbers - Coding Challenge)

Write a function called delete_starting_evens() that has a parameter named my_list.

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

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

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

My code:

def delete_starting_evens(my_list):
for number in my_list:
if number % 2 == 0:
my_list.remove(my_list)
return my_list

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

print(delete_starting_evens([4, 8, 10]))


I am getting the following output:
[8, 11, 15]
[4, 7, 9]

Why is the “8” and “4” still printing?

We must be cautious when .removeing items from a list we are iterating.

Aside

A good many learners are focused on ‘removing even numbers from the front of the list’ and overlooking the gorilla in the room: Where is the first odd number?

>>> def delete_starting_evens(my_list):
...     for x in my_list:
...         if x % 2:
...             my_list = my_list[my_list.index(x):]
...             break
...     return my_list
... 
>>> delete_starting_evens([4, 8, 10, 11, 12, 15])
[11, 12, 15]
>>> 
1 Like