Https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/remove-even-nums

Hey everyone! I’ve been trying to solve this challenge but I can’t seem to find the solution. I tried the ‘Give me the solution’ button, but I still don’t understand whats happening in the function. Could someone please explain to be how this works? Thanks. Heres the code from the solution:

def delete_starting_evens(lst):
while (len(lst) > 0 and lst[0] % 2 == 0):
lst = lst[1:]
return lst

I just wrote this explanation today for another user so I’m just going to copy and paste it…:

I’ll unwrap this code the inside out.

Action to do, do it while the given 2 conditions are True :

lst = lst[1:]

  • Set lst to be the value of list minus its first value. Example: [a,b,c] would become [b, c]. We can also say that we are removing the value at index-0 from lst.

What are the 2 conditions?

len(lst) > 0

Length of lst is greater than 0

lst[0] % 2
The item at index-0 is even. The equation x % 2 = 0 is proof of a numbr being even.

So if the first number of a list is odd, one of the conditions won’t be true, and nothing will happen.

PS: remember to format your code with the </> button before pasting :slight_smile:

1 Like

Ahhh! Thanks! :smile:

1 Like

Any time! Happy that it helped.

Just one more question, how does the function make sure anything after index 0 is even? wouldn’t lst = lst[1:] just be the list without its index value zero?

x % 2 = 0 is basically saying, if x is divided by 2, there is no remainder (meets criteria of even numbers). The while loop keeps asking this condition every time after the code block is run.

It will stop running once they’re not even numbers (or the list is empty). So [2,3,4] becomes [3,4]. In contrast, [2,4,3] would become [3]. If it the list is just [2] it would become [].

But if you set it to lst[0], wouldn’t it just see if the index value zero in lst is even? Or does it look through everything else after that?

Sorry for all the trouble :sweat_smile:

No worries that’s what the forums are for!

Nothing is being set to lst[0]. The value in that slot is just being checked for even-ness every time. It stops once it reaches an odd number.

so if we put a print statement to track the while-loop it might look like this

lst = [2,4,3]
# code runs
# lst[0] is even, so now lst is set to [4,3]
# lst[0] is even, so now lst is set to [3]
# lst[0] is odd, so now the loop breaks, lst remains [3]

but isnt one of the condition this?
I’m really sorry

No need to be sorry :slight_smile:

This code block: while (len(lst) > 0 and lst[0] % 2 == 0):
is just a condition, it only evaluates to whether it’s true or not.

lst = lst[1:] is the only code that is executed, given those conditions.

Try pasting this code into a python compiler and see if it helps visualize better.

def delete_starting_evens(lst):
   while (len(lst) > 0 and lst[0] % 2 == 0):
      print("This is the list before: {}".format(lst))
      lst = lst[1:]
      print("This is the list after: {}".format(lst))
      return lst

delete_starting_evens([2,4,3])

This is line of code i’m confused about. I understand its just a condition but i’m confused because one of the conditions is lst[0] % 2 == 0 . Doesn’t it mean that if only index 0 is even then the condition will pass? How does this make sure that the indexes after zero are even or not? Also, by removing index zero from lst, doesn’t that also remove the first number from lst?

That is correct. However, the reason it repeats until the first number of the list is even is due to the while loops itself. A while loop will repeat until the given condition is false.
So, with the first iteration, the loop checks to see if the first element of the list is even, if so, it removes it. Then it runs again. And so on until the first element isn’t even (or there is an empty list).
I hope this helps!

Oh! So it doesn’t just keep looping on index zero? It moves to the next index?

Well it keeps looping on index-0, except that because the previous index-0 gets cut it’s a different value each time.

1 Like