We want to write a function that removes all the odd numbers in a list.
This is what I finally tried:
def eliminate(x):
for i in range(0, (len(x)-1)):
if x[i] % 2 != 0:
x.remove(x[i])
print(x)
eliminate([1,2,5,6])
I get the error: "List index out of range!"
I'd really appreciate it if you could help because it's driving me crazy that I can't figure out what I'm doing wrong. I tried pop
and delete
methods too. But I don't know what I'm doing wrong here. It's definitely the 4th line, though. Any ideas?!