Azfar Faizan - 1 year ago 64

Python Question

I have two lists

`a = ['E300' 'E407' 'nan' 'nan' 'nan' 'nan' 'nan' 'nan' 'nan' 'nan' ]`

b = [ nan nan nan nan nan nan nan nan nan nan nan nan nan]

I wanted to remove nan from the lists. So I used this on list 'a' and 'b'

`a = [x for x in a if x != 'nan']`

b = [x for x in b if x != 'nan']

So it works fine for list 'a' because I think it was taking 'nan' as a string but not with list b. How can I remove those nan from list b. Thanks in advance.

Answer Source

When you don't put it in quotes, `nan`

is a number (it's a special number used to represent a nonexistent result, called "Not a Number"). It's so special that it doesn't even equal itself. Use `math.isnan`

or `numpy.isnan`

to test if a number is `nan`

```
b = [x for x in b if not math.isnan(x)]
```