def delete_starting_evens(lst): # new_list = lst for index in range(len(lst)): while lst[index] % 2 == 0 and len(lst) > 0: lst.pop(index) return lst
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
The program works for the first list but fails as soon as I have a list that only has even numbers. I get "IndexError: list index out of range"
If I change the condition to
len(lst) > 1" it returns the list , but I fail to delete the last item with this method and the logic escapes me.
I have edited the initial conditions in the while loop to check if len(lst) > 0 first, and now it works.
I thought the two conditions must both be true