That should work. We check whether the number is NaN or not using math.isnan .
We insert an item into list1_clean and list2_clean if none of the items in the source list is NaN . To test this, we use the any function, which returns True if any element of iterability is True .
>>> list1 = [[1, 2, float('NaN')], [3, 7, 8], [1, 1, 1], [10, -1, float('NaN')]] >>> list2 = [1, 2, 3, 4] >>> from math import isnan >>> list1_clean = [elem for elem in list1 if not any([isnan(element) for element in elem])] >>> list1_clean [[3, 7, 8], [1, 1, 1]] >>> list2_clean = [list2[index] for index, elem in enumerate(list1) if not any([isnan(element) for element in elem])] >>> list2_clean [2, 3]
To reduce size and avoid using zip , you can do
>>> cleanList = [(elem, list2[index]) for index, elem in enumerate(list1) if not any([isnan(element) for element in elem])] >>> cleanList [([3, 7, 8], 2), ([1, 1, 1], 3)] >>> list1_clean = [elem[0] for elem in cleanList] >>> list2_clean = [elem[1] for elem in cleanList]
any function β
any(...) any(iterable) -> bool Return True if bool(x) is True for any x in the iterable.
isnan function β
isnan(...) isnan(x) -> bool Check if float x is not a number (NaN).