Removing even numbers from a list using the modulo operator should be simple. if num % 2 == 0 then remove from the list. However, it works as expected only when the list is sorted using the sorted python function:

Blockquote

lst1 = [1, 2, 10, 4, 20, 5, 3, 21, 40]

for num in lst1:

if (num % 2) == 0:

lst1.remove(num)

print(lst1)

output: [1, 10, 20, 5, 3, 21]

If the list is sorted using the lst1.sort() the result is still not correct:

Blockquote

lst1 = [1, 2, 10, 4, 20, 5, 3, 21, 40]

lst1.sort()

for num in lst1:

if (num % 2) == 0:

lst1.remove(num)

print(lst1)

output: [1, 3, 5, 20, 21]

The only way to get the expected result is to use the sorted() python function

Blockquote

lst1 = [1, 2, 10, 4, 20, 5, 3, 21, 40]

for num in sorted(lst1):

if (num % 2) == 0:

lst1.remove(num)

print(lst1)

output: [1, 5, 3, 21]

I tried the above code using Python 3.8.5 and 3.9.2 but the results are always the same. As far as I know, a list of integers should not be sorted before working with it. So what is the problem?