Codecademy Forums

FAQ: Code Challenge: Loops - Delete Starting Even Numbers

27 posts were merged into an existing topic: Why doesn’t iterating with for work while removing items from a list?

3 posts were split to a new topic: Infinite loop [solved]

4 posts were split to a new topic: How does break work? [solved]

8 posts were split to a new topic: Code challenge lists - improving my solution [solved]

3 posts were split to a new topic: Using continue in a while loop [solved]

I have a question
The following code below worked for me but i’m not exactly sure how.

def delete_starting_evens(lst):
for first_number in lst:
if lst[0] % 2 == 0:
lst.pop(0)
delete_starting_evens(lst)

return lst

It would be great if you explained to me how this worked and if this is a “good” way to solve problems like these.


the code above is the same code with indentation

Removing from the front of a list is as much work as making a copy of the whole list.
If you’re going to remove from a list, do it from the end. Otherwise, create a new list with the values you wish to keep.
Your function would be more efficient if you reversed the list, popped values from the end, then reversed it again. But it would be better yet to make a copy.