Ask Your Question

Revision history [back]

Depends what is meant by 'NAN' values.

Depending on whether they are strings or floating-point NaN values, different tests would detect them.

If they are strings:

 zz = [x for x in z if x != 'NAN']

Depends what The question is meant by 'NAN' values.about filtering a list, keeping only those elements in the list that correspond to positive numbers.

Depending on whether they This means filtering out all elements that are not positive numbers.

In particular all strings or floating-point NaN values, different tests would detect them.must be filtered out.

If they the list is known to consist only of numbers and 'NAN', or only of numbers and any strings, it is easy to filter out strings.

To exclude values that are strings:equal to the string 'NAN':

 zz = [x for x in z if x != 'NAN']
'NAN' and x > 0]

To exclude all strings:

zz = [x for x in z if not isinstance(x, str) and x > 0]

If list elements can be anything, first check which elements are real numbers, and test positivity for those.

import numbers
zz = [x for x in z if isinstance(x, numbers.Real) and x > 0]