 HEllo guys!
In the python code challenges: loops, in Computer Science course,
problem number 3. Delete Starting Even Numbers:

Write a function called `delete_starting_evens()` that has a parameter named `lst` .

The function should remove elements from the front of `lst` until the front of the list is not even. The function should then return `lst` .

For example if `lst` started as `[4, 8, 10, 11, 12, 15]` , then `delete_starting_evens(lst)` should return `[11, 12, 15]` .

Make sure your function works even if every element in the list is even!

This was asked. I have written a code as follows…

``````def delete_starting_evens(lst):
modified_list = lst
for num in modified_list:
if num % 2 == 0:
modified_list.pop(0)
else:
break
return modified_list

#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
``````

But the output of this code gives me this result:

``````[11, 12, 15]

``````

The result should have been:

``````[11, 12, 15]
[]
``````

Hi @courtmcd,

It’s always problematic if you try to modify the same object you’re iterating through, what used to be element `n` is suddenly element `n-1` or similar. I’d suggest trying a solution that doesn’t rely on that kind of behaviour as it is very hard to keep track of.

As an aside `modified_list = lst` just copies the reference to the same original object. If you wanted a new shallow copied list use the list class `new_list = list(first_list)` or slicing `new_list = first_list[:]`.

1 Like

Thanks @tgrtim !!! Yes, the solution provided by them used a `while loop` … but I wanted to try with a `for loop` and was not successful. Thanks !

There’s different ways to do it, they’re just all more complicated than using the while loop. Here’s a way that doesn’t stop the loop:

def delete_starting_evens(lst): modified_list = [] odd = False for num in lst: if num % 2 == 0 and not odd: continue else: modified_list.append(num) odd = True return modified_list #Uncomment the lines below when your function is done print(delete_starting_evens([4, 8, 10, 11, 12, 15])) print(delete_starting_evens([4, 8, 10]))

You can iterate through the list indexes to end the for loop when the work is done.

def delete_starting_evens(lst): modified_list = [] for index in range(len(lst)): if lst[index] % 2 != 0: modified_list.extend(lst[index:]) break return modified_list #Uncomment the lines below when your function is done print(delete_starting_evens([4, 8, 10, 11, 12, 15])) print(delete_starting_evens([4, 8, 10]))

Thanks! It clarified my doubt. Also could you please tell me why do you set `Odd = True` after defining the function? Could you tell me it’s uses and how it helps improve the code?

1 Like

Without adding a true false variable, the for loop would remove all even numbers instead of leading even numbers. Something is needed to tell the function that an odd number has been found. You could use the modified list to do this as well, there’s usually multiple approaches to completing a multi step programming problem.

`if num % 2 == 0 and not odd:` does nothing for each even number while `odd` is still false. Once it finds a number that is odd, it runs the else block when swaps odd to true. Subsequent even numbers will not trigger the if block due to the odd variable being true so that every number in the list will be added.

To go through it conceptually:

``````Create an an output list
Having seen no numbers, we have seen no odd numbers
Iterate through numbers in the submitted list
If the number is even and we have not seen an odd number
continue to the next iteration
in all other cases
add the number to the output list and record that we have seen an odd number
output the new list
``````

You should also learn to be very concise in solving problems so that you don’t accidentally miss nuances, though here we’re experimenting for some extra knowledge. By creating a new list and returning that we don’t change the original list, so technically we don’t get the answer right. def delete_starting_evens(lst): modified_list = lst[:] for num in modified_list: if lst % 2 != 0: break else: lst.pop(0) return lst list_1 = [4, 8, 10, 11, 12, 15] #Uncomment the lines below when your function is done print(delete_starting_evens(list_1)) print(list_1)
1 Like