I'm doing some practice work and I am tasked with removing the first number from a string until it is odd.
The function should remove elements from the front of lst until the front of the list is not even. The function should then return lst.
For example if lst started as
[4, 8, 10, 11, 12, 15]
, then delete_starting_evens(lst) should return[11, 12, 15]
.
def delete_starting_evens(lst):
for i in lst:
if i % 2 == 0:
lst.pop(0)
else:
break
return lst
print(delete_starting_evens([4, 8, 6, 6, 6, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
def delete_starting_evens(lst):
for i in lst:
if i % 2 == 0:
lst = lst[1:]
else:
break
return lst
print(delete_starting_evens([4, 8, 6, 6, 6, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))`
The code works as intended if i use lst = lst[1:]
,
but I do not understand why the lst.pop(0)
version does not work. It will work for a few iterations, but then no longer pops.