Python Remove All Numbers From A List
I have a list of unicode elements and I'm trying to remove all integer numbers from It. My code is: List = [u'123', u'hello', u'zara', u'45.3', u'pluto'] for el in List: if is
Solution 1:
You list consists of unicode strings which are no instances of int
obviously. You can try converting them to int
in a helper function and use this as a condition to remove them/ to construct a new list.
def repr_int(s):
try:
int(s)
return True
except ValueError:
return False
original_list = [u'123', u'hello', u'zara', u'45.3', u'pluto']
list_with_removed_ints = [elem for elem in original_list if not repr_int(elem)]
Post a Comment for "Python Remove All Numbers From A List"