How to remove all integer values from a list in python

To remove all integers, do this:

no_integers = [x for x in mylist if not isinstance(x, int)]

However, your example list does not actually contain integers. It contains only strings, some of which are composed only of digits. To filter those out, do the following:

no_integers = [x for x in mylist if not (x.isdigit() 
                                         or x[0] == '-' and x[1:].isdigit())]

Alternately:

is_integer = lambda s: s.isdigit() or (s[0] == '-' and s[1:].isdigit())
no_integers = filter(is_integer, mylist)

Leave a Comment