My school assignment requires me to read a list from the user and then remove all odd elements from it. The loop I'm using to check for odd numbers doesn't even iterate through all the list elements.
ans=1
lst=[]
while ans!=0:
no=int(input('Enter value for list or press 0 to exit:'))
if no!=0:
lst.append(no)
else:
break
print('\nYour list is:',lst)
for i in lst: # loop to check for odd nos
print('i is', i)
if i%2==1:
lst.remove(i)
print('List after removing odd elements:',lst)
In the second loop, I added the print statement to check the inconsistent output and here are the results: Output
Some of the list elements are skipped(?) when iterating and so they aren't removed which is giving me the incorrect output. Why could this be happening?