0
number_list = input("enter integers: ").split()
integer_list = [int(item) for item in number_list]

for i in integer_list:
    if i % 2 == 0:
        integer_list.remove(i)

print(integer_list)

this is the code that I came up with but when even numbers are repeated like: 2 2 2 2 2 3 4 5 then it will not remove all the 2's.

Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23

1 Answers1

0

You should never modify a list while you are iterating over it. This will lead to the iteration skipping entries, as you've seen. Instead, use the filtering built into list comprehensions:

odd_numbers = [n for n in integer_list if n % 2 != 0]

gives [3, 5]

Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23