-2
li = [5,6,77,45,22, 2 ,12,35,57]
for i in li:
    # print(i)
    if (i%2 == 0):
        print(i)
        li.remove(i)
    else:
        pass

print(li)

why my output is coming this [5, 77, 45, 12] after writing above code

Michael Szczesny
  • 4,710
  • 4
  • 13
  • 31

1 Answers1

0

append the odd numbers to a new list instead

li = [5,6,77,45,22, 2 ,12,35,57]
li2 = []
for i in li:
    if (i%2 != 0):
        print(i)
        li2.append(i)
    else:
        pass

print(li2)
stefan_aus_hannover
  • 1,068
  • 7
  • 12