Delete Starting Even Numbers

Hi Everyone,
I am struggling with the coding challenge: “Delete starting even numbers”

This was my attempt while using a for loop, I later read the hint which indicated the use of a while loop but my result is still the same. My function can’t seem to return an empty list. If the last element is even, this fails.

#Write your function here def delete_starting_evens(lst): for i in lst: if lst[0] % 2 == 0: lst.remove(lst[0]) 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]))

I’m not looking for an answer as much as I’m looking for some hint that will correct my understanding of the logic of what I’ve written.


This is a common consideration iterating with for-loops. What are we iterating on? Usually it’s good to be crystal clear about start and stop conditions (and occasionally step conditions such as if we want to skip indices, or iterate backwards). Other loop-type or loop-like options may be more useful in certain cases: e.g. while-loop, for-loop with range-based iteration, or even recursion etc.

As additional visual help, you can try putting formatted statements in the beginning of your for loop and after your remove operation. Prints of interest might be the status of the list, and the item being iterated on.

1 Like

Hi again,
This is as far as I seem to have gotten.

def delete_starting_evens(lst): counter = 0 while counter < len(lst): if lst[0] % 2 == 0: lst.remove(lst[0]) counter += 1 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]))

Now from breaking down this while loop, what I’m understanding is my condition is no longer true by the third element. My counter is at 2 while my len of list is 1. Hence exiting the loop. I don’t seem to understand the consideration with iterating. I have tried to increase the len of lst to + 1, and this works on the 3 element list where its even. However, I run into an infinite while loop when I try this with another list.

I now know that my condition is failing but I can’t seem to wrap my head around a solution. Is there something obvious I am missing?

Edit: After looking at the solution, I can see where I went wrong. My logic was somewhat ok, but I was missing a big part of what was causing my empty case to fail. Since the counter was bound to go over the length, I should have been checking if the list had an element in it (>0) as well as if the first element is even. Seems pretty silly looking back at it!

I think the concept you are struggling with is how list deal with the fallout of removed elements. Lets say we have the following list:

a   b   c # the list
0   1   2 # indexes/positions of elements in the list

and I remove 'a' from the list, we get:

_   b   c 
0   1   2 

I used an underscore to show the absence of the removed value. Except the list can’t exists in this state, so everything to the right of the removed elements will shift to the left:

b   c
0   1

If we add a loop, which will go to the next element every time, you might see why this shifting + loop is problematic

there are a couple solutions, including checking if the list had an element in it.

other options include: not increasing the counter.
or simple go until you find an odd number, and then use a slice

there are several possibilities

Yup, this is exactly what I was struggling with! I really do appreciate the time you took to lay out the examples, everything seems to be clicking for me. For some reason I made the false assumption that if I were to use a while loop, a counter needed to be there. But I was wrong.

Anyhow, I now realize that the index changes as elements are removed from a list. Thanks again!

Sort of, kind of… The index doesn’t change, it remains a serial sequence. The position of subsequent elements is what changes, and furthermore, the index pointer (the iteration variable of a for loop) still advances as normal. It does not stay back to point to the next value that has just slid into that slot, so that value gets skipped.

In a while loop, there is no index pointer, save what we provide. The key is to make sure the value of the pointer is unchanged in any iteration which removes an element from the list. If no element is removed, then, and only then, increment the pointer.