List index out of range

im having a tough time learning loops, could anyone tell me why my solution doesnt work?

#Write your function here
def delete_starting_evens(lst):
if len(lst) > 0:
while lst[0] % 2 == 0:
lst = lst[1:]
return lst

I can see how checking both the len and % in one line is better but im not sure why mine doesnt work as they both look very similar

#def delete_starting_evens(lst):
#while (len(lst) > 0 and lst[0] % 2 == 0):
#lst = lst[1:]
#return lst
#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

1 Like
>>> [10][1:]
[]
>>> [][0]
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    [][0]
IndexError: list index out of range
>>> 

Study this as it relates to your code. The answer is in there.

1 Like