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.

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

https://www.codecademy.com/journeys/computer-science/paths/cscj-22-intro-to-programming/tracks/cscj-22-fundamentals-of-python/modules/cscj-22-python-code-challenges-i/articles/python-code-challenges-loops

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:]
        my_list.pop(0)
    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:
            break
    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.