"del list[0]" does not delete

Hello! I’m trying to solve this problem: https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/remove-even-nums

I wrote this code:

def delete_starting_evens(lst):  
  for num in lst:    
    if lst[0] % 2 == 0:
      del lst[0]
    else:
      break  
  return lst 

This code works, but if all number os the list is even, the lst returned will not be empty. Example:

input:
print(delete_starting_evens([2, 4, 6]))

output:
[6]

Why 6 is still there?

clearly things got deleted. the two first values are gone.
it would also seem like you only used it twice, which would mean the problem isn’t with deletion, but in how you decide when and if to use it

also keep in mind that removing from the front involves moving all the following values to fill the gap (which is an unreasonable amount of work)
lists support adding and removing things at the end, not the start

2 Likes