I’m just trying to understand why .pop() is unable to remove the remaining element from a list to make it empty.
def delete_starting_evens(lst): for num in lst: if num % 2 == 0: lst.pop(0) return lst print(delete_starting_evens([4, 8, 10, 11, 12, 15])) print(delete_starting_evens([4, 8, 10]))
The result I get is:
[11, 12, 15]
 #was expecting this to be empty
- The first result was expected, however, I was expecting the second result to be an empty list. What am I missing here?
- For the solution code noted below, wouldn’t the last iteration return an out of range index error? During the last iteration on the second list of all even numbers, the while loop conditions would be met, but wouldn’t the lst[1:] be out of range since the only element left is at 0?
def delete_starting_evens(lst): while (len(lst) > 0 and lst % 2 == 0): lst = lst[1:] return lst
Thanks in advance for your help!