Python Code Challenges: Loops - 3. Delete Starting Even Numbers

Link to question:
[https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-code-challenges/articles/python-code-challenges-loops](https://link to question)

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]))
for number in lst:

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.

2 Likes

When you write lst = lst.pop(0), lst actually becomes the first element of the list, because .pop() returns the removed element:

lst = [1,2,3]
x = lst.pop(0)
print(lst)
>> [2,3]
print(x)
>>1
2 Likes

would this statement be the same as the slice:
lst.pop(0)
or would it react funny?

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”

At the very least, the function is expected to return an empty list.

1 Like

If you just wrote that on it’s own, then yes, you’d get the same thing as a slice.

2 Likes