Clear! thanks @lab-xi
Hi there,
Thanks for the methods.
I think the hint provided by codecademy is quite misleading.
Your method makes much more sense.
Here’s there hint:
Use a while loop to check two things. First, check if the list has at least one element, using len(lst)
. Second, check to see if the first element is odd using mod ( %
). If both of those are True, slice off the first element of the list using lst = lst[1:]
.
#Write your function here
def delete_starting_evens(lst):
counter=0
for i in lst:
if i%2==0:
counter+=1
continue
lst=lst[counter:]
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]))
So, I’m having a really hard time grasping the code given for this section even though I’ve gone through it several times.
**1. 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!
def delete_starting_evens(lst):
for i in lst:
if len(lst) % 2 != 0 and lst[:-1] % 2 != 0:
return lst
So my logic is the first check if the list if it has an odd number of items then set another condition to parse through the elements to verify that they’re not even. In my mind, I thought an IF statement made more sense based on how the problem is worded so i could compmrehend it. Maybe my reasoning is off and my code is just making it more complicated.
Also I was thinking would using CONTINUE be an easier way to negate the even numbers in the list to get the same result?
Any help on this would be appreciate, I’ve been stuck on this all day and even a detailed explanation of the finished wasn’t too much help. :S
Any logic that contains a return better be well thought out. The above is not in that category. Go back to the question, and start over would be my advice.
Like I stated, I’ve been going through the solution all day which is why I’m asking for a possible alternative to the solution code given or a better explanation of the solution.
What is the return statement doing in the loop? What will the effect be? Don’t walk away from your unsolved code. Find out why it doesn’t work.
The return in a loop is a sign that sticks out. Were I a teacher I would say erase this and start over.
Now assuming you have resolved the return
problem, what significance is there to the parity of the length in removing even numbers?
if len(lst) % 2 != 0
Ok, this helps and gives me a starting point. Thanks
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.
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. 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
Solved
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 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.