# Delete from list using a loop

Here is my code:

``````def delete_starting_evens(lst):

for i in lst:
if i % 2 == 0:
del i

return lst

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

Here is the output:

``````[4, 8, 10, 11, 12, 15]
[4, 8, 10]
``````

Here is the desired output:

``````[11, 12, 15]
[]
``````

To me, my logic makes complete sense… if the remainder is 0 then it must be a even number so delete that even number then return the list…

You are correct on how to tell if a number are even, but you should check which numbers you are comparing. Try printing out what `i` is.

Also in the current state your `for` loop is looping through every number, and from what I understand you only want to delete starting evens. How can you make your code stop if the current number is not even.

hint

If a `return` statement ends the function, perhaps an `else` gate paired with `return` can help the code decide when to stop.

Also your `del` is not being told properly what to delete, it must be given a list index, and the list to delete from:

``````a = [1, 2, 3]
del a[0]

print(a) #prints [2, 3]
``````

edit:
Do also consider that if you remove the front item from a list than the indexing changes:

``````a = ['a', 'b', 'c', 'd']
``````

index looks like this:

``````  0    1    2    3
['a', 'b', 'c', 'd']
``````

However after using `del a[0]`, the index looks like this:

``````  0    1    2
['b', 'c', 'd']
``````

Okay so I managed to tell it what to delete

``````for i in lst:

if i % 2 == 0:

del lst[0]

return lst
``````

Which has given the output

``````[11, 12, 15]
[10]
``````

Which is still not correct.

I’ve tried using a while loop but that threw a syntax error
I’ve tried

``````  for i in lst:
if i % 2 == 0:
del lst[0]
else:
return lst

``````

I understand what you’re saying with the index changes

At this point it feels like I’m guessing and I want to understand what I’m doing

You are almost correct, the only thing I see wrong is the positioning of your `else` gate.
Since Python code blocks are paired by indentation, you’re `else` gate is not currently indented correctly, so it is not being used with your `if`.

Which part are you confused about?

List doesn’t exactly support removing values in arbitrary locations, instead deletion is a combination of several other operations – moving all the following values and then resizing to be 1 smaller:

``````def del(ls, i):
for j in range(i+1, len(ls)):
ls[i - 1] = ls[i]
ls.pop()
``````

It’s probably not what you mean.
The task isn’t about making deletions on an existing list, it’s about obtaining a list where even values are not included.

A better strategy would be to move the values to keep to the front, and then resize it, or, better yet, create an entirely new list with only the values to keep.

In terms of how many steps it should reasonably involve, you should only iterate through the list once or twice, so for example a list of size 1000 should take about 1000 steps, but with what you’re doing you’ll end up with closer to 500000 steps as a result of moving everything around on each deletion rather than putting each thing where it should go and then never moving it again.

As has been mentioned, the indices change when you move things around and you would have to adjust for that each time you do it. But it’s kind of pointless to fix that bug since it’s the wrong thing to do anyway.

1 Like

I think the way you used DEL is not right because del is used with the index of the item.