As with most problems, there are many ways to solve this one. It can be done with for
or while
.
Here’s your code with a few tweaks:
#sample lists
a = [2, 4, 5, 8, 9]
b = [2, 4, 6, 8, 10]
c = [1, 2, 3, 4, 5]
def delete_starting_evens(lst):
for num in range(len(lst)):
if lst[num] % 2 == 1: #looking for the first odd number
return lst[num:] #return list from current index to end when odd number is found
return [] #if we get to here, no odd number was found, so the whole list is even, and we return an empty list
#test the function with 3 sample lists
for x in [a, b, c]:
print(delete_starting_evens(x))
Output:
[5, 8, 9]
[]
[1, 2, 3, 4, 5]