Code Challenge Loops

Hi, I have a question regarding this challenge : https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/remove-even-nums

When there is only 1 item in a list, does this item is number 0 ?

Because I dont understand why my code is not working as expected :

#Write your function here

def delete_starting_evens(lst):

  for num in lst:

    if lst==[]:

      break

    elif (lst[0]%2)==0:

      lst.remove(lst[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]))

I have an error with the second test, my code return : [10] instead of .

If you read and write to the same thing, the writes will affect the reads. You might want to read from one thing and write to another.

Also consider what list.remove does. It iterates through the list, searching for a value.
The thing you’re doing should only involve one total iteration through the list (or a few, but not growing with larger input, a constant amount)
Since you’re using list.remove many times, you are also iterating through the list many times.

So how can you do this without looping through the list many times?

1 Like