I'm currently learning Python and one of the tasks is the following:
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!
It was easy to make using a while loop:
def delete_starting_evens(lst):
while len(lst) > 0 and lst[0] % 2 == 0:
lst = lst[1:]
return lst
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
However, I wanted to test using a for loop and after debugging a bit, I struggle to find why is 8 being skipped from the list:
def delete_starting_evens(lst):
for i in lst:
if i % 2 == 1:
break
else:
lst.remove(i)
return lst
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
I know it must be something really simple but I've been burning brain cells for a bit and would love if someone can shed a bit of light into it. Thanks