Coding Challenges loop, I solved it. But I think my solution is wrong,

Hello: I have a problem, I found a solution to the problem in this Link
It is the 3rd question for loops coding challenge in python. The prompt in question is as follow

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!

I found a solution that gave me an output that the system greenlit, but I think the while loop I’m trying to use is not actually iterating through the list items. And In the body of the loop I’m just slicing the list through the range I’m providing.

#Write your function here

def delete_starting_evens(lst):

  while len(lst)> 1 and lst[0] % 2 == 0:
   lst = lst[3:]
   return lst
#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]))

I think that in my ‘’’ lst = lst[3:] ‘’’ snippet I am actually just slicing the list at that index value. Although The output met the requirements. I don’t think it’s the right code. Cuz it’s just cutting off the list at the range value. As I decreased the index value. And the list got sliced at that index value. Even though, In my view it should not.

3 seems like too special a number to mention in your code

whether the input has at least two elements is not relevant. if there’s a leading even value then it should get removed regardless of the size of the list

when you slice a list, you are making a new list, maybe that’s something that should only happen once rather than inside a loop

you can use the print function to write out information about your progress while the code is running


You are correct — your loop is not iterating through the list. It is looking that two conditions are met and then slicing the list. So, why not change it so that it does iterate through the list items?

Your biggest handicap here is that you can see the tests, and they both have the same three elements in the beginning of their lists: [4, 8, 10, 11, 12, 15] and [4, 8, 10]. So, while your function technically “works” in these two cases, it would fail if another list were used as lst. For example, try passing your function the lists below:

  • [2, 3, 4, 5]
  • [2]
  • [2, 4, 6, 8, 10, 12, 14]
  • [3, 4, 5, 7]
  • [2, 20, 4, 40, 9]

If you can change your function so that it handles each of these lists correctly, you should be good to go.

Happy coding!