Comparing lists containing NaNs
<p>I am trying to compare two different lists to see if they are equal, and was going to remove NaNs, only to discover that my list comparisons still work, despite <code>NaN == NaN -&gt; False</code>. </p>

<p>Could someone explain why the following evaluate <code>True</code> or <code>False</code>, as I am finding this behavior unexpected. Thanks,</p>

<p>I have read the following which don't seem to resolve the issue:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/20320022/why-in-numpy-nan-nan-is-false-while-nan-in-nan-is-true">Why in numpy <code>nan == nan</code> is False while nan in [nan] is True?</a></li>
<li><a href="http://stackoverflow.com/questions/10034149/why-is-nan-not-equal-to-nan">Why is NaN not equal to NaN? [duplicate]</a></li>
</ul>

<p>(Python 2.7.3, numpy-1.9.2)</p>

<p><strong>I have marked surprising evaluations with a <code>*</code> at the end</strong></p>

<pre><code>&gt;&gt;&gt; nan = np.nan
&gt;&gt;&gt; [1,2,3]==[3]
False
&gt;&gt;&gt; [1,2,3]==[1,2,3]
True
&gt;&gt;&gt; [1,2,nan]==[1,2,nan]
True ***
&gt;&gt;&gt; nan == nan
False
&gt;&gt;&gt; [nan] == [nan]
True ***
&gt;&gt;&gt; [nan, nan] == [nan for i in range(2)]
True ***
&gt;&gt;&gt; [nan, nan] == [float(nan) for i in range(2)]
True ***
&gt;&gt;&gt; float(nan) is (float(nan) + 1)
False
&gt;&gt;&gt; float(nan) is float(nan)
True ***
</code></pre>

<p>To understand what happens here, simply replace <code>nan = np.nan</code> by <code>foo = float('nan')</code>, you will get exactly the same result, why?</p>

<pre><code>&gt;&gt;&gt; foo = float('nan')
&gt;&gt;&gt; foo is foo # This is obviously True! 
True
&gt;&gt;&gt; foo == foo # This is False per the standard (nan != nan).
False
&gt;&gt;&gt; bar = float('nan') # foo and bar are two different objects.
&gt;&gt;&gt; foo is bar
False
&gt;&gt;&gt; foo is float(foo) # "Tricky", but float(x) is x if type(x) == float.
True
</code></pre>

<p>Now think that <code>numpy.nan</code> is just a variable name that holds a <code>float('nan')</code>.</p>

<p>Now why <code>[nan] == [nan]</code> is simply because <code>list</code> comparison first test identity equality between items before equality for value, think of it as:</p>

<pre><code>def equals(l1, l2):
    for u, v in zip(l1, l2):
        if u is not v and u != v:
            return False
    return True
</code></pre>

python list comparison