This exercise is asking to remove starting even numbers in a given list until it reaches an odd number and then return the list. In case the list has only even numbers then function should return empty list []
. For example,[4, 2, 3, 6]
the function should return [3, 6]
. In the second case, [2, 4, 6, 8]
the function should return []
.
The function works fine for the first case. However, for the second case, when I pass the list [4, 8, 10]
the function returns [10]
(with square brackets) instead of []
.
This is the function I built:
def delete_starting_evens(lst):
for i in lst:
i = lst[0] //initialise with first index in every loop.
if i % 2 == 0 :// remove the even number.
lst.remove(i)
return lst
The output:
[10]
Instead of []
.
I understand that I can do it better using While loop, but I want to have full understanding of both loops.