Please help me understand why when I feed a list that has more than one element to this function, it leaves one behind, but when I feed it with just one element, it successfully removes it.
def delete_starting_evens(my_list):
for numbers in my_list:
if my_list[0] % 2 == 0:
my_list.remove(my_list[0])
return my_list
print(delete_starting_evens([4, 8, 10]))
print(delete_starting_evens([8, 10]))
print(delete_starting_evens([10]))
Output:
[10]
[10]
[]
I expected the output of all 3 prints to be \[\]
. I have also tried this with .pop
and del()
with identical results. I realize a cleaner way to write this would be to use a while loop and "remove" the first element with my_list\[1:\]
instead, but I wanted to understand the logic and discrepancy behind my attempt with .remove