You are modifying the list while iterating it. It will skip elements because the list changes during the iteration. Removing items with list.remove()
will also remove the first occurence of that element, so there might be some unexpected results.
The canonical way of removing elements from a list is to construct a new list, like so:
>>> def check(temp):
... return list(x for x in temp if not isinstance(x, str))
Or you could return a regular list comprehension:
>>> def check(temp):
... return [x for x in temp if not isinstance(x, str)]
You should generally test for types with isinstance()
instead of type()
. type
has no idea about inheritance for example.
Examples:
>>> check(['a', 'b', 1])
[1]
>>> check([ 1, 'a', 'b', 'c', 2 ])
[1, 2]
>>> check(['a', 'b', 'c', 'd'])
[]