I struggled a lot with this one and ended up into something that I would appreciate you giving me your opinion about its efficiency and if it can be called an accepted solution to this exercise.
def delete_starting_evens(lst):
try:
while lst[0] % 2 == 0:
lst.pop(0)
return lst
except:
a = []
return a
I used try: and except: creating a new empty list addressing the pop() error where it can’t work at a single element list due to not having anything to replace it with.
That is a really nice way to present a single return. The topic came up in another thread today, along similar lines. Serendipity that you would post an extraordinary example.
Your while-loop will never make more than one iteration, how is that different from an if-statement?
You’re using two conditions, but one is the inversion of the other. You only need to test that condition once.
Removing at the front involves copying all the following elements in order to move them 1 step closer to the beginning. Doing this repeatedly could be argued to be the wrong thing. It would be better to only write the values to keep to a new list, or to make the removals in a single operation after having identified what location the removal stops at.
def delete_starting_evens(lst):
# find location of first non-even
for i, x in enumerate(lst):
if x % 2 == 1:
# found the location of interest, remove up to here and exit
# no such location found, remove it all.
The new version you have confuses me a bit. How would it work in a list of even and odd numbers? What would make the iteration stop at the first odd? Also why test for Mod 1 and not 0?