Python Challenge Loops , Delete Starting Even Numbers - Found the answer, don't get the logic behind

def delete_starting_evens(lst):
  i = 0
  while i in range(len(lst)):
    if lst[i] % 2 == 0:
      lst = lst[1:]
      i = i + 1
  return lst

Exercice in question

Hello guys,

So I finally found what seems to be the solution to this problem but when I think about what the program does step by step there’s something I don’t get:

  • While i is in the range of the full list : so the full list, at that time, contains n elements, and i starts with 0
  • the program goes on, the list get sliced, and finally i gets incremented by 1. Now the list has n-1 elements, and i = 1. And we loop back to start.

This is where I don’t get it. Because in this new loop, i would look for the position 1 in the new list, but in this new list, the position 1 is held by what used to be the third element in the unsliced list, aka the former position 2. So I would assume that by writing the code this way, i would be skipping the element in between from the list. Yet it doesn’t.

What am I thinking wrong please?

Hope I was clear enough … Thanks anyway =)

I “think” it has to do with the fact that you’re reassigning lst to lst[1:] so every time your loop runs, this part: range(len(lst)) the lst refers to the reassigned lst. That’s why it doesn’t skip. But I may be wrong.

Thanks for your answer.

What you say is what I understood too, but that’s exatctly why in my distorted mind something should then be skipped.

Let’s take the list lst = [2, 4, 5, 6, 7, 10] . It has 6 elements
i=0 , and while i in range(len(lst)):meaning "while i is in the range of 6 elements. It is, because i = 0, so i is assigned to the first position which is the number 2 in the list.

The program checks if i is an even number. Since it is, the program slices the list to remove this even number.
The updated list is then lst = [4,5,6,7,10] . It has 5 elements in it.
Then, and this is where I’m going, i gets incremented by 1. now i = 1, so when we loop, the program should be checking the element in position 1 in the updated list. The position 1 in the updated list is 5.
So in my logic, the number 4 in the updated list is skipped. But in the program it’s not.

Or is it?

1 Like

Well it is actually the solution I gave and the answer was validated.

The actual codecademy answer is rather more elegant and doesn’t involve any temporary variable such as i .

This doesn’t change my interrogation though =)

I removed i = i + 1 from the code and also got the same result.

Maybe the answer is that by looping, the variable i never gets really updated and stick to 0 … ? But then how come i remains 0 but my list, on the other hand, got indeed updated ?

Gonna think about it some more.

My conclusion is i just stays at 0, and so you essentially have an infinite loop that breaks when the first element isn’t even.
You don’t even need an i.

def delete_starting_evens(lst):
  while 0 in range(len(lst)):
    if lst[0] % 2 == 0:
      lst = lst[1:]
  return lst

Because the point of your function is to remove the first element until it starts with an odd number.

Because of your if statement. It checks if the first element is even, and if so, lst = lst[1:] You know your i = i +1? it comes after your continue, so that’s why i stays 0 and never is updated. And why you can take that out and it still works. The loops reruns (continues) before it processes that.

And your dilemma about it skipping? Would only happen if your i = i + 1 was placed before your continue.

1 Like

Have a quick recap for yourself of exactly what the continue statement is supposed to do. Have you checked inside the loop with a print statement or similar for what value i has whilst the loop repeats? It’s by far the easiest way to check what’s going on and if it actually agrees with what you wanted to happen.

1 Like

This problem can be solved without mutating the list.