# 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 % 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 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 % 2 == 0` of the condition) or the list is empty (`len(lst) > 0`)

3 Likes

oh, I see. Thank you so much.

``````def delete_starting_evens(lst):
while lst % 2 == 0 and len(lst) != 0:
del lst
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` 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 % 2 == 0 :
del lst
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, 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 % 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!

Hello

Im having a lot of trouble with this excercise, specialy abut the slicing part. Im not sure im doing it right.

Can someone give me a hand

I attach the code below

Think about the problem. Do we really need to modify the list at all? Not really.

Hint

What will follow the starting even numbers? Answer: An odd number, or nothing if the list is all evens (or empty).

Let’s say we’re looking at a meme that wants us to find a word. Given that we know what the word is, we train our eye to see it, even in a chaos of letters. Eventually our eye sees it, and is able to slice it out. We don’t actually remove it. Our version of it is, virtual.

A Python slice is pretty much the same thing. It’s virtual until we assign it or do something with it.

Given the simple syntax,

``````iterable[start : end - 1 : step]
``````

we can get a virtual snapshot of any sequence within the sequence. We can even get a snapshot of any sequence within that one.

1 Like

How come the suggested solution uses a complicated while loop instead of a for loop such as :

``````def delete_starting_evens(lst):
for num in range(0,len(lst)) :
if lst%2==0:
lst.pop(0)
return lst
``````

Did you test your code with,

• a list of all even numbers
• an empty list

and get an empty list as the return value?

1 Like

First of all, thank you for this great example.
My question:

why can’t I write:
`for i in new_lst` instead of `for i in lst`?

If I do that, the outcome is:
[11, 12, 15]

looping over the same array as you are removing items from, can lead to all sort of trouble

you could run your code here:

http://www.pythontutor.com/visualize.html#mode=edit

to see which problems this create. (you can step through it, and inspect your program at every step)

lets say we are in the first loop iteration, and the item has to be removed. Now we have an empty slot/space in our list, which can’t be, so everything shifts one position to the left:

``````4   6   8   10 # values
0   1   2   3 #indexes
.pop(0
_   6   8   10 # _ represents empty value (which we just removed)
0   1   2   3
``````

but the list can’t have empty values, so this happens:

``````6   8   10
0   1   2
``````

but now, the following/second iteration of the loop, the loop moves on to the next index (1). Skipping the value which has moved to index/position 0.

1 Like

Thank you for your explanation! That’s a great link!
In the Python document it is also said that it is “simpler and safer to create a new list instead”.
https://docs.python.org/3/tutorial/datastructures.html

Hello, after many attempts i finally got one which i can get the desired results but there’s still something wrong with the code (if not everything lol)
here’s what i did:

``````#Write your function here
def delete_starting_evens(lst):
i=0
for num in lst:
if num % 2 == 0:
i += 1
else:
break
if i == len(lst):
return print("The list is either empty or entirely made of even numbers!")
else:
lst = lst[i:]
return lst
``````

and here’s the outcome:

``````[11, 12, 15]
The list is either empty or entirely made of even numbers!
**None**
``````

i wanna know from where this None is coming from!
also, point me all the flaws of my code plz ;c

There is no situation where your function should be printing anything, and there is no situation when your function should return something other than a list.

1 Like