FAQ: Code Challenge: Loops - Delete Starting Even Numbers

well, the first step would be to gain information about the problem:

#Write your function here
def delete_starting_evens(lst):
  for index in range(len(lst)):
    while lst[index] % 2 == 0:
      lst = lst[index+1:]
      print(lst, index)
      if lst[index] % 2 != 0:
        break
    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 added a print statement, allowing us to see where things go wrong. If all the elements in the list are even, after removing the last element from the list, you still try to access by index, which results in IndexError

def delete_starting_evens(lst):

while (len(lst) > 0 and lst[0] % 2 == 0):

lst = lst[1:]

return lst

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

In this code, I have a question. If the length of the list is greater than 1 and if the first number on the list is an even number, the list starts from the second number. However, when I printed the list of numbers, when the first number was a even number, it didn’t print from the second number. It just printed 11, 12 ,15, not 8, 10,11,12,15. Can someone help me? Thank you.

I am confused, you got the right code. The task is not to start at the second number when first number is even. As mentioned in the instructions:

The function should remove elements from the front of lst until the front of the list is not even

I think you misunderstood the objective.

but doesn’t lst[0] mean the first number in lst, and lst[1:] mean numbers starting from the second number? How does this mean removing elements from the front of 1st until the front of the list is not even? Thank you.

Yes, but this process happens many times (thanks to the loop). So you walk through the steps, lets say we have this list:

[4, 8, 10, 11, 12, 15]

then the list condition is true:

5 > 0 and 4 % 2 == 0

so the first element gets removed.

then the loop condition is checked again:

4 > 0 and 8 % 2 == 0

which again is true, so again, the first element of the list (which is now 8), gets removed and so forth until an odd number is encountered ( lst[0] % 2 == 0 of the condition) or the list is empty (len(lst) > 0)

oh, I see. Thank you so much.

def delete_starting_evens(lst):
  while lst[0] % 2 == 0 and len(lst) != 0:
    del lst[0]  
  return lst
 
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

Hi could someone explain why I am getting an index error: list out of range for the second case (The error occurs when the lst becomes empty). Even if I use the list splicing method of the deleting the number with lst = lst[1:] it still remains. Thanks!

for the second case, once all the items have been removed, the loop has to check the condition again, which gives an error for lst[0] given there is no element at this index

1 Like

Thank you! that clears things up. I made the revision below to account for that and it worked.

def delete_starting_evens(lst):
  while lst[0] % 2 == 0 :
    del lst[0]  
    if len(lst) == 0:
      break
  return lst
 
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

I was wondering, I also checked the solution code (below) provided and also uses lst[0], however the same error I encountered did not occur. How come that did not happen? Is it because the first condition was false which is why the while loop terminated I assume without checking the second case? I tried the solution code changing the position of each condition and the same error the one I asked occured.

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

yes, once a condition is false when using and, the comparison stop (same for if A and B), because there is no way the condition is going to become true.

Good to know. Thanks again!