FAQ: Code Challenge: Loops - Delete Starting Even Numbers


This community-built FAQ covers the “Delete Starting Even Numbers” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Delete Starting Even Numbers

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


I don’t quite grasp how the provided solution function works (see solution), specifically with lst = lst[1:]

If a list parameter were to consist of only 1 number–as becomes any list consisting of only even numbers as it is processed by the loop function–which would have an index of 0, how does using the slicing language lst[1:] function properly in this case?


i think the “for i in lst” statement makes the program iterate through every component and len(lst)>0
condition makes sure before the loop starts to run both of the conditions are met; so if lst has only one component , it will satisfy the condition of len(lst)>0 beside the modulo operator condiditon and while loop will run.


I used “lst.pop(0)” instead of “lst = lst[1:]”, then I got the wrong result. Does anyone can tell me why? Thank you!

#Write your function here
def delete_starting_evens(lst):
–for i in lst:
----if i % 2 == 0:
–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]))

[11, 12, 15]


Hi, I have some question on this one.
1)Does lst[0] means that every number in the list are processed by the loop?
2) Why is the code “len(lst) > 0” crucial in the loop?


Why this does not work??

def delete_starting_evens(lst):
    for i in lst:
        if i % 2 > 0:
    return lst



i think one of the function calls provided by the exercise can help:

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

8 isn’t remove, this problem occurs when you have two even numbers after each other in the list and you remove from the same list as you are looping over


This could be wrong im a noob but I think this is why:

  1. lst[0] is basically selecting the 0 ‘index’ in the list = [1, 2, 3] int 1 is index 0, int 2 is index 1 etc … so when it loops after slicing the first even number

lst = lst(1:)

the lst index 0 will also change! Our lst here will become lst = [2, 3] with 0 index now being int 2. The loop will then iterate again and again until…

This … 2) len(lst) > 0 is crucial because if we don’t we enter in to an infinite loop? or something imagine if the list was all even integers once the loop has checked all the integers it has none to check which I think would create an infinite loop.

Sorry I could be a million percent wrong! But thought i’d try for my own benefit as well as yours!

Happy coding :slight_smile:


Could anyone please paste systematic solution code here?
When I rewrite on my own, I could never see the correct lines again.


The general idea is to find the first ODD number in the list, and make it the first element of the returned list.


hi guys, i had some trouble with this too but i think i figured it out. the instructions may be a bit misleading but essentially, our job is to remove all the even numbers in “lst” before the first odd number.

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]))  #prints [11, 12, 15]
print(delete_starting_evens([2, 4, 5,  8, 10]))  #prints [5, 8, 10]

the condition for the While loop to be active is when the len(lst) is greater then 0 and lst[0] is divisible by two.

when active, the purpose of the While loop is to “reset” the parameter variable “lst” as lst[1:], which is the 1st element onwards, because the 0th element is divisible by 2 so it is left out.

this happens repeatedly until the While loop hits the first odd number in “lst”, then the function ends.

notice that the indent for “return lst” is flushed with the while loop. this means that if the given list of numbers starts with an odd number right off the bat, it will return the original lst. otherwise, this will return the remaining list inclusive of the first odd number and the rest of the numbers behind it (regardless odd or even).

please correct me if I am found to be wrong! I’m just happy to see that I’m not the only confused one around here. :slight_smile:


huge part of programming figuring out what exactly the requirement is. In a job you could have a client which says: I want this feature (no technically information), and you have to figure it out. Maybe not so extreme in a junior role, but in a senior role that is very realistic.

so i think the exercise is good practice to get started with understanding the problem

In your program, if the list starts with a lot of even numbers, you end up with quite a few lists.


Which works but does reset the list every time the first number is even. Much quicker and more efficient is to find the index of the first odd number and return the slice with that as the first element. There will be two returns, one in the loop, and one at the end.


Sir, how can we do it with for loop? Please suggest .


What are you asking? Did you skip the lesson on loops and go straight to the challenge?


Sir, can you please explain me this code. link ==https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/python-functions-loops-cc/exercises/remove-even-nums


Please post what code you have so far.


def delete_starting_evens(lst):
while (len(lst) > 0 and lst[0] % 2 == 0):
lst = lst[1:]
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]))


Is that your code or the given solution? What is it that you do not understand?


I don’t understand how this given is working for the question given here.