Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python check if all elements in a list are Nan [duplicate]

Tags:

python

nan

numpy

My code sometimes produces a list of nan's op_list = [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]. I want to know if all elements are nans.

My code and present output:

  op_list = [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]
  print(np.isnan(op_list))
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True])

My expected output:

   True
like image 763
Mainland Avatar asked Dec 11 '25 23:12

Mainland


1 Answers

You need all:

np.isnan(op_list).all()
# True

For a solution using lists you can do:

all(i != i for i in op_list)
# True
like image 63
yatu Avatar answered Dec 13 '25 11:12

yatu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!