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

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.

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)

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

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.

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.

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.

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.