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?
When I run your function on this list: [2, 4, 6, 8, 10, 12], it should return an empty list.
It returns: [8, 10, 12]
Also, [1, 2, 3, 4, 5, 6] returns [3, 4, 5, 6] even though there were no ‘starting evens.’
you are right! frankly im trying to get the intuition behind while and for (when to use “while” or “for”)the lessons haven’t gone deeper on it. i think that if we (or should i say I) get the intuition the rest would be easy. thanks for your eventual response