Code challenge lists - Accounting for an all even list?

cant seem to figure out what is wrong with this code?

def delete_starting_evens(lst):
  if len(lst) > 0:
    i = 0
    while lst[i] % 2 == 0 and i < len(lst):
      lst = lst[i + 1:]
      i += 1
    return lst
  else: 
    return [ ]

thanks in advance

1 Like

the lesson provide us with some function calls:

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

maybe they can shed some light on the situation? Seems they can, given i get an error.

the error is caused by this one:

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

what is different about this list? Maybe insert some print statements in your function to see where it goes wrong?

1 Like

i tried with some print statements, and i found that moving the condition (i < len(lst)) from in the definition of the while loop into the while loop itself did it:

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

that returns the empty list for the second print statement as requirerd.
thank you!

1 Like

Do you also understand why this prevents the index error?

1 Like

i think so, it’s because with my first attempt, the loop removes the first element each time if it is even but with a list of all evens eventually there would be nothing to remove after they are all removed, so the slicing inside the while loop would be referring to a non-existing index. am i right?

1 Like

A for-loop already iterates over values, there’s no need to fiddle with indices
You do use indices for slicing, but you shouldn’t be doing that anyway because it’s copying multiple elements where you should only be processing one (wrong amount of work)

I also suggest checking for the special case and returning immediately instead of nesting all code inside the if-statement. Try to keep things flat, hug the left side, don’t walk off to the right.
And, uhm, is that special case really that special? What if iterating over an empty list created an empty result instead?

This should reduce that code length into roughly half, and more importantly, remove a lot of flow control which leaves you with something much simpler.

2 Likes

Oh and. Bug on this input:

[0, 0]

Should return an empty list shouldn’t it? It doesn’t.

2 Likes

wow, didn’t think that the extra flow control could be averted, i will retry the exercise now! as for the bug, with the current code, you could add a condition: lst[i] != 0 in the while definition right?

thank you

1 Like

0 is no special case, just another even number

Don’t try to patch up something that’s fundamentally broken! Look at what’s being done wrong instead.

1 Like

will do. thanks for all your help!

1 Like

If you write down some numbers on a piece of paper you can study how you yourself solve the problem manually. Your code should probably match very closely.

2 Likes

I’m not sure what you disagree with or what your counter-argument is :^)

1 Like

You seemed to be asking if it was reasonable.
Upon reading it, there are some things that come to mind about how it could be changed, this is without putting any thought into it, it’s a reaction.

If I write a math expression like so:

((((((1))))))

Then you’d have a similar reaction, you’d want to change it into

1

before doing anything else, there is no reason to write it that way. After cleaning it up, it’ll be easier to continue.

I don’t think it’s inappropriate to note that something could be written simpler, especially when at the same time motivating how it could be done.

1 Like

Do you intimidate easily? That would be the only reason to shy away.

First rule in learning anything is to be able to take criticism. It’s how we learn from our peers, or future peers (the ones we see as mentors today).

Second rule in learning is never be afraid to be wrong.

Third rule is never be afraid to admit it and take note of others’ contributions in pointing it out.

Not that any accusations are being leveled here by any stretch. Just the ravings of an old man cast upon the waters.

2 Likes