FAQ: Code Challenge: Loops - Delete Starting Even Numbers


#1

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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!


#2

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?


#3

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.


#4

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:
------lst.pop(0)
----else:
------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]))

output:
[11, 12, 15]
[10]


#5

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?


#6

Why this does not work??

def delete_starting_evens(lst):
    for i in lst:
        if i % 2 > 0:
            break
        else:
            lst.remove(i)
    return lst

Blockquote


#7

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


#8

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:


#9

Mods please check if this is right? Many thanks