FAQ: Code Challenge: Loops - Delete Starting Even Numbers

oh i removed the print and just returned an empty list, many thx.
(idk why i felt like returning a message)

If there was an invalid input (for example: maximum of empty list)
then you would not want the outcome to be interpretable as a valid result of any kind

def maximum(xs):
    if not xs:
        raise ValueError('got empty sequence, there is no maximum')

If you’re going to use something once then you probably don’t need a variable for it.

1 Like

can someone tell me where i’m going wrong? the first print statement works fine, but the second give me the following error:

Traceback (most recent call last):
File “script.py”, line 14, in
print(delete_starting_evens([4, 8, 10]))
File “script.py”, line 4, in delete_starting_evens
while lst[0] % 2 == 0:
IndexError: list index out of range

i’ve research this a bit, googled the error, but i’m still lost. maybe even post the simplest, most straight forward solution to this challenge??? i’ve been on this one for over an hour.

thanks much!

def delete_starting_evens(lst):
  if len(lst) > 0:
    for i in lst:
      while lst[0] % 2 == 0:
        lst.remove(lst[0])
      return lst
  else:
    return []
  
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

print(delete_starting_evens([4, 8, 10]))

If you consider what list.remove does, it probably isn’t appropriate here.

It iterates through the list, searching for a matching value, and then, it moves all the values that come after the match.

That’s quite a lot of work, it’s as much work as rebuilding the whole thing. And you’re using it multiple times. If you have a list with a million even values in it, then you would remove from the front a million times, each removal would be a million steps, for a total of a thousand billion steps.

You’re testing whether the input is empty. This is redundant, that’s not a special case. If the input is empty then you would remove zero starting evens.

If you read your error message it says index out of range. So… what index did you use, and which indices are valid for that list at that time?

I don’t find the way to use enumerate, so i run .remove instead , it works anyway:
def delete_starting_evens(lst):

i = 0
while len(lst) >=1 and lst[i]%2 ==0:

    lst.remove(lst[i])

i+=1

return lst

Hi can you please explain this concept to me please? I dont understand why we have to start from lst[1:] ?? its bugging me. what happens if we type in lst[0:] or lst[2:] ?? why does it give me either 2 empty lists or [11,12,15]

we start the slice at a different point in the list

if a number is even, we want to remove it. So we take the remaining list (second element till end). The second element is positioned at index 1.

thats what im stuck at, why does the slice start from secodn element, arent we supposed to tell while to start from the 1st element?

No, given we want to remove the first item. Which we achieve by slicing from second item till the end.

1 Like

You’re repeatedly copying all but the first value.

(and yes, repeated copying is not the most efficient thing ever, better if you copy from the location you will keep in the final result)

1 Like

ok so what i understood is that the while loop will take the first element until condition is met and when condition is false it will slice from there? thanks for your time!!! i have a better understanding but i dont fully grasp this one i dont know why…

No, the loop will remove the first element of the list (by taking all other elements) for as long as the first number in the list is even.

1 Like

What you would want to be doing here is to consider what operations a list supports and how much work each one takes to carry out, and then use some of those operations together in a way that overall adds up to a reasonable amount of work, a similar amount of work to what it would take for a human to carry it out manually.

The basic operations you have are to read and modify anywhere, add or remove at the end, creating an empty list, obtaining its length. You could implement your solution from those operations, and if you need some more powerful operation then you could implement it as a separate function based on those basic operations. For example, copying would involve creating a new list, and then repeatedly adding values to the end of it, you would obtain those values from the original from which you can read anywhere so you’d start at the beginning and read to the end by counting up from 0 towards its length.

2 Likes

wow that makes so much more sense now. thanks a lot!!!

thanks so much!! this gave me a better outlook. you are the best

def delete_starting_evens(lst):

for i in lst:

if i%2==0:

  lst.remove(i)

return lst

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

print(delete_starting_evens([4, 8, 10]))

what’s wrong in this code? Why the 8 is being returned?

What side-effects does this action have?

Hi!

I finally managed to find a solution, here it is:

def delete_starting_evens(lst):
  while lst[0] % 2 == 0:
    if len(lst)>1:
      lst.remove(lst[0])
    else:
      lst = []
      break
  return lst

Like any other exercise there are probably several ways to solve it and I haven’t read the thread yet since I don’t want to see any spoiler but do you guys have any idea what I can improve in this code?

If you can point me in the right direction so I can do a little bit of work by myself before I eventually read the thread.

#Alternative working method

def delete_starting_evens(lst):
  a=[]
  f=False
  for i in lst:
    if i%2!=0:
      f=True
      a.append(i)
    elif f==True:
      a.append(i)
    else:
      continue
  return a

What makes this alternative solution good? Improved performance? Readability?