The challenge is to create a function that deletes even numbers from a list. The answer code is here:
def delete_starting_evens(lst):
while len(lst) > 0 and (lst[0] % 2 == 0):
lst = lst[1:]
return lst
I don’t understand what line 3 is doing. Why does the list slice from index 1? Why not lst[0:]? I don’t understand how list slicing works in this context.
The slice is removing the first element (index 0) by taking everything from the second element (index 1) till the end of the list
1 Like
I read an explainer - to clarify then, if both things on line 2 are True, then line 3 replaces that because it’s an even with everything else, essentially removing the even figure?
The condition (line 2) and the removal of the first element (line 3) work together.
part of the condition on line two is that the first element is even, if that is true, remove the first element (line 3)(by making a copy of the remaining elements in the array)
2 Likes