It's generally a bad idea to modify a list that you're iterating over, all sorts of strange things can happen. The particular strange thing that's happening here has to do with the way Python iterates over lists.
Think of Python maintaining the position of the current item for iterating. When you delete an item, the others all shift "left" in the list but Python still advances the position. That means, if you have two odd numbers in a row (like 15
and 99
)(a), deleting the 15
moves 99
to the left (to where the 15
was) but the next iteration will be looking at where the 99
was before shifting, not where it is now.
For example, consider the list [1, 3, 6, 8]
, these are the steps Python will take:
List Pos newList newPos comment
---- --- ------- ------ -------
[1, 3, 6, 8] 0 [3, 6, 8] 1 Odd 1, delete, advance.
[3, 6, 8] 1 [3, 6, 8] 2 Even 6, leave, advance.
[3, 6, 8] 2 [3, 6, 8] 3 Even 8, leave, advance.
[3, 6, 8] 3 iteration finished.
You can see that consecutive odd numbers cause a problem here, it will only delete alternate ones.
As to the solution: if, as your code suggests, you just want the even numbers in the list, you can use the much more succinct (and Pythonic) list comprehension (no need even for a function to do this):
myList = [10, 2, 3, 4, 5, 80, 15, 99]
evens = [item for item in myList if item % 2 == 0]
# gives [10, 2, 4, 80]
And, for completeness, since your function name seems to indicate you want the highest even number, that would be something like:
biggestEven = max([item for item in myList if item % 2 == 0])
# gives 80
(a) Your problem actually has nothing to do with the fact the 99
is at the end of the list, any consecutive odd numbers, anywhere in the list, would cause the same issue.