-1
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,911
  • 5
  • 15
  • 32

1 Answers1

1

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,777
  • 12
  • 13