CODE CHALLENGE: LOOPS Delete Starting Even Numbers

Can someone please explain the logic of the code required to complete this exercise? I don’t understand how the below tells the program to forget the first three elements of the list.

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/python-functions-loops-cc/exercises/remove-even-nums

def delete_starting_evens(lst):

while (len(lst) > 0 and lst[0] % 2 == 0):

lst = lst[1:]

return lst

I’ll unwrap this code the inside out.

Action to do, do it while the given 2 conditions are True:

lst = lst[1:]

  • Set lst to be the value of list minus its first value. Example: [a,b,c] would become [b, c]. We can also say that we are removing the value at index-0 from lst.

What are the 2 conditions?

len(lst) > 0

Length of lst is greater than 0

lst[0] % 2
The item at index-0 is even. The equation x % 2 = 0 is proof of a numbr being even.


So if the first number of a list is odd, one of the conditions won’t be true, and nothing will happen.

1 Like