In Python, bool
is a subclass of int
:
>>> isinstance(True, int)
True
>>> isinstance(True, bool)
True
This means that you need a more refined check. You could, for example, check that type(item) == int
.
For background, see PEP 285:
6) Should bool inherit from int?
=> Yes.
In an ideal world, bool might be better implemented as a
separate integer type that knows how to perform mixed-mode
arithmetic. However, inheriting bool from int eases the
implementation enormously (in part since all C code that calls
PyInt_Check() will continue to work -- this returns true for
subclasses of int). Also, I believe this is right in terms of
substitutability: code that requires an int can be fed a bool
and it will behave the same as 0 or 1. Code that requires a
bool may not work when it is given an int; for example, 3 & 4
is 0, but both 3 and 4 are true when considered as truth
values.
Another, unrelated, issue is that you're modifying a list while iterating over it. Have a read of Modifying list while iterating and links therein.
This results in a subtle bug in your code. For example, it fails to remove "b"
from the following list (try it!):
list_a = ["a", "b", 1, 2, 3]
for item in list_a:
if not isinstance(item, int):
list_a.remove(item)
One clean way to fix this is by using a list comprehension.