# Delete Starting Even Numbers

I can’t delete the last element in a list using pop()
here is my code:

``````#Write your function here
def delete_starting_evens(lst):
lst.pop(0)
for i in lst:
if i % 2 == 0 and i>=0:
lst.pop(0)
else:
continue
return lst

#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15])) #[11, 12, 15]
print(delete_starting_evens([4, 8, 10])) #[10]
``````

The code works on the first list, but it returns [10] for the second list

Try throwing a print statement so you can see what’s happening in your iteration.

Also, don’t need the first pop (the one outside the for-loop) as you don’t know for sure the first item is even.

Welcome to the forums

I guess the 1st pop() is there in case the given list starts with an odd number, and I added the output of each print statement.

The exercise writes:

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` .

That means if one considers a base case that’s a list that has a starting number that’s odd —> in that case you can just return the list without running any more code.

Ok, I get it now.
But if all the numbers in a list are even, can I delete the last remaining number using pop() ??

Try `print(lst)` at the beginning of your loop. You might see something interesting about how for-loop is iterating. You can use pop, but if you use that technique you’d have to adjust how you use the for-loop.

I’ve tried this one but still got the same result([10] for the 2nd list) :

``````#Write your function here
def delete_starting_evens(lst):

for i in lst:
if i % 2 == 0 and i>=0:
lst.pop(0)
elif i%2 ==0 and len(lst) == 1:
lst = []
else:
continue
return lst

#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
``````

Have you tried iterating over the range of the list? That way you are still guaranteed to get all the cycles you need.

Note you can also use slicing, and there’s always the option of dumping the end result in a temp list (but I don’t recall it being fully necessary, but it’s still a good option if to get it working at first).

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.