It’s been very common to see users modifying the list as they iterate over it. One may question the wisdom of modifying when using a range in a for loop. A while loop is more flexible since it is not affected by changes in length.
My thinking on this problem is to just look for the first odd number in the list.
>>> def remove_starting_evens(x):
for i, n in enumerate(x):
if n % 2: return x[i:]
>>> remove_starting_evens([2, 4, 6, 8, 3, 6, 9, 12])
[3, 6, 9, 12]
>>> remove_starting_evens([2, 4, 6, 8])
Ah - haven’t learned enumerate yet. From the documentation, I guess that “i” is the index, and “n” is the value, but you can set the index as a different starting value within the function, i.e. enumerate(x, 1) if you want to start at an index of 1 rather than 0?
For clarity, am I reading it correctly as the following pseudocode?
for index, value in enumerate(x):
if value % 2 returns a value of 0, continue with index+1, otherwise: return x[index:]