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

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 [].

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]

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!