This function will repeatedly remove the first element of a list until it finds an odd number or runs out of elements. It will accept a list of numbers as an input parameter and return the modified list where any even numbers at the beginning of the list are removed. To do this, we will need the following steps:
Define our function to accept a single input parameter
my_list
which is a list of numbersLoop through every number in the list if there are still numbers in the list and if we haven’t hit an odd number yet
Within the loop, if the first number in the list is even, then remove the first number of the list
Once we hit an odd number or we run out of numbers, return the modified list
I wrote a function called delete_starting_evens()
that has a parameter named my_list
.
The function should remove elements from the front of my_list
until the front of the list is not even. The function should then return my_list
.
For example, if my_list
started as [4, 8, 10, 11, 12, 15]
, then delete_starting_evens(my_list)
should return [11, 12, 15]
.
My code:
def delete_starting_evens(my_list):
for i in my_list:
if i % 2 == 0:
my_list.remove(i)
else:
break
return my_list
a=[4,8,10,12,14,15,16,17,19,20]
print(delete_starting_evens(a))
The problem is that the function was removing the even indexes. I was expecting that the function should delete the even numbers until it finds the odd number or runs out of elements. How to solve it?