Skip to content
Advertisement

Remove elements from python list if they contain special characters

I have a python list of lists, where each row index represents a list of values. In some instances, the row values contain special characters. In the case that any list element in the row contains special characters, I want to remove that entire row from the list. Note that I want to do this without converting the list into a NumPy array or pandas data frame. I was thinking of checking the row index that contains special characters and then just removing them this way but not sure how to do it with just a python list. The other alternative is to convert into a NumPy array, do the data cleaning, and then convert back into the original list format but maintaining the original structure.

testList = [[30.0, '?', 910.0, 120.],[11.0, 25.4, 330.3, 340.0], [1.6, 23.4, 23.0, 46.0], [7.0,14.0,?,2.0], ['*', '*', 8.9, 6.4]]

newList = [[11.0, 25.4, 330.3, 340.0], [1.6, 23.4, 23.0, 46.0]]

Advertisement

Answer

You can use any within list comprehension to filter out lists that have strs using isinstance:

>>> testList = [[30.0, '?', 910.0, 120.],[11.0, 25.4, 330.3, 340.0], [1.6, 23.4, 23.0, 46.0], [7.0,14.0,'?',2.0], ['*', '*', 8.9, 6.4]]
>>> [subL for subL in testList if not any(isinstance(val, str) for val in subL)]
[[11.0, 25.4, 330.3, 340.0], [1.6, 23.4, 23.0, 46.0]]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement