Delete Starting Even Numbers - Loops

You’re welcome. Just don’t walk away from this problem, Show us that you have solved it. You’ve got a couple decent hints to go with, now make it your own.

I was taking a step back to get a better perspective on the problem and find another solution. I had every intention of solving it. I just don’t like moving onto different lessons until I’ve fully grasped everything that the current problem is asking.

What will the return value (a list, of course) have as its first element? That is the question to solve around. Seek that value and go from there. I always focus on what the function will return.

1 Like

If we’re told the first element cannot be an even number, then there is no other option than to look for the first odd number. Make that the first element and everything that follows is undisturbed, just in a new list.

Trying this but but it excludes all even numbers within the list, I’m thinking there may be a way to put another condition for a break once it iterate to the first odd number. And having PRINT with the loop produces NONE when the function is called so there’s that too. -_-

def delete_starting_evens(lst):
  for i in lst:
    if i % 2 == 0:
      continue
    print(i)

But I think that would make the code convoluted.

Now clear your mind. Write a list of numbers on a piece of paper at random. Now go through the list and find the first odd number. How did you do that? By starting with the first, then second, then third, until you hit that one. That is what your code should do.

You have actually hit upon it but need to turn the logic around. Don’t look for a short cut, look for the exit ramp.

Was able to figure it out with help from a friend. He explained it really well to me so I can finally move onto the next part. :smiley: I know both codes work but this one just makes more sense to me for some reason.

def delete_starting_evens(lst):
  for i in lst:
    if i % 2 == 0:
      lst = lst[1:]
    else:
      break
  return lst
1 Like

Solved
remove_first_even_answer

if you don’t check the len(lst) > 0 first, you get an index out of range error, is that expected? Is it happening in the while statement? i.e. if lst[0] % 2 ==0 and len(lst) > 0 throws an out of range error w a list of all evens

Hello all,

Seeking some help. Not really seeing why my code doesnt work .

def delete_starting_evens(my_list):
for number in my_list:
if number % 2 == 0:
my_list.pop(0)
elif number % 2 != 0:
return my_list
else:
return my_list

print(delete_starting_evens([2,4,6,8,11,13,15]))

#output

[6,8,11,13,15]

You’re not looping through the entire length of list you supplied. The way your code is now, the conditions are met with the if statement, so just the first two items are removed from the list.
more on .pop() here.

So, how would one do that (loop through a list) with a list of any length? Also, remember that the function should not take an empty list. So, you might want to include that logic in the function too.

See some above code for possible hints/answers or use :mag: as there are several posts on this very topic.

Also, please format your code b/c it’s difficult to read as is.

You could also check out Python Tutor to visualize what your code is doing.

Would like to address these one at a time.

‘You’re not looping through the entire length of list you supplied.’ If you look at the following function is also a loop,
but it doesnt need a len function to iterate through the entire list. tbh i have never heard of a loop needing or requiring a len to iterate through the list.

def example_loop(a_list):
for number in a_list:
if number > 3:
print(number)

example_loop([1,2,3,4,5])

output

#4
#5

‘Also, remember that the function should not take an empty list. So, you might want to include that logic in the function too.’
none of the parameters have empty lists.