input_list = [22,456,3465,456,6543,546,345]
for num in input_list:
if num==0 or num%2==0:
input_list.remove(num)
Could you please tell me what is the problem in this code? It is not removing second 456 from the list.
input_list = [22,456,3465,456,6543,546,345]
for num in input_list:
if num==0 or num%2==0:
input_list.remove(num)
Could you please tell me what is the problem in this code? It is not removing second 456 from the list.
The problem with your code is that you are removing an item while iterating over the list.
So when the num become 22
then 22
will be removed and 456
become index 0
in your list, in the next iteration the for loop looks for index 1
which is 3465
.
Try this:
input_list = [i for i in input_list if i%2 == 1]