Claims the second list is returning “None” instead of " " I checked some other forum posts and unsure of the difference between suggested solution and this one. Is it that the for-in statement is acting funny with the list where the suggested solution of a while statement reacts differently?
Suggested solution:
def delete_starting_evens(lst):
while (len(lst) > 0 and lst[0] % 2 == 0):
lst = lst[1:]
return lst
Also, I originally did this using the list pop() method, however it wasn’t working (same to this one). Is there a difference between using pop(0) instead of the slice?
Thanks for your input
def delete_starting_evens(lst):
for number in lst:
if (number % 2 == 0 and len(lst) > 0):
lst = lst[1:]
else:
return lst
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
When the list is empty this loop does not run. That’s just the half of it. There are other issues with what is returned. Consider the criterion and ponder whether it is being fully applied.
I think you’re saying that after the for-in statement removes the last element in an all evens list, the return lst else statement would never run, thus … “None”
You may have noticed in earlier posts that mutating a list while we iterate has a consequence, namely shifting the list to the left and thereby skipping the value that just slid into the vacant spot.