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

I’ve made a different answer, and I just can’t seem why it fails deleting the last number of the list in the second print()

# My code: def delete_starting_evens(lst): for num in lst: if num % 2 == 0: lst.pop(0) else: break return lst # Exercise's code: print(delete_starting_evens([4, 8, 10, 11, 12, 15])) print(delete_starting_evens([4, 8, 10]))

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.

2 Likes