Simple Loop Question

I have a pretty basic question about the answer to this challenge.
The goal is to continue removing each starting number of a list if it is even until the starting number is odd.

Main question:
Are the indices relative?
Also, if they are, is there a way to make them not relative?

Answer given:
def delete_starting_evens(my_list):
while (len(my_list) > 0 and my_list [0] % 2 == 0):
my_list = my_list [1:]
return my_list

Thank you for you help.

Python Code Challenges: Loops
Challenge 3: Delete Starting Even Numbers

Are the indices relative?

Nope. Kind of counter to the idea of an index, no?

The code is clever, but perhaps too much so. You can just pop, which I feel is clearer:

def delete_starting_evens(my_list):
    while (len(my_list) > 0 and my_list[0] % 2 == 0):
        # rather than creating another object an reassigning
        # my_list = my_list [1:]
    return my_list

If you wanted to use that splatish method, you’d do better to grab you index position first. e.g.

def delete_starting_evens(my_list):
    for pos in range(len(my_list)):
        if my_list[pos] % 2 == 1:
    return my_list[pos:]

Now you’re not thrashing your array variable all the time, you’re just returning a single new one.

1 Like

Thanks for your response.
Using pop makes sense.