Hi am currently working my through the Python course.
I have just been working on a problem that got the better of me so I looked at the solution.
After breaking down the solution I am still struggling to understand why it works in one case and not the other.
It’s the Delete Starting Even Numbers challenge.
The function should remove elements from the front of
lstuntil the front of the list is not even. The function should then return
For example if
[4, 8, 10, 11, 12, 15], then
[11, 12, 15].
Make sure your function works even if every element in the list is even!
The solution is this.
def delete_starting_evens(lst): while (len(lst) > 0 and lst % 2 == 0): lst = lst[1:] return lst
This is the inputs that were provided.
print(delete_starting_evens([4, 8, 10, 11, 12, 15])) print(delete_starting_evens([4, 8, 10]))
I understand how this works on the first set of inputs.
While the length of the list is greater than zero, and the list index position 0 can be divided by two with no remainder, the lst is then the lst from the next position. This then iterates through the loop again.
However I am unsure of why this works on the second set of inputs as both criteria are met so wouldn’t it surely output an empty list? I know this is not the case, however I am struggling to understand why?