title: What is the proper way to deal with `float('nan')` in Python sets?
body: <p>The number <code>float('nan')</code> causes some serious problem in Python sets. For instance, look at the following:</p>
  
  <pre><code>S = set()
  S.add(float('nan'))
  S                    # {nan}
  S.add(float('nan'))
  S.add(float('nan'))
  S                    # {nan, nan, nan}
  len(S)               # 3
  </code></pre>
  
  <p>Sets are supposed to have only one entry of each type, but each <code>float('nan')</code> has been added as a separate item. But things get even more complicated when you try to remove them.</p>
  
  <pre><code>float('nan') in S    # False
  S.remove(float('nan'))
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  KeyError: nan
  </code></pre>
  
  <p>OK, every test against <code>float('nan')</code> evaluates to <code>False</code>, even <code>float('nan') == float('nan')</code>, but the consequences for sets are counter-intuitive (at least to me). So, what to do in practice about the following three problems:</p>
  
  <ol>
  <li>How to know the number of <em>different</em> objects in a Python set given that <code>float('nan')</code> may have been added?</li>
  <li>How to know if a set contains <code>float('nan')</code>?</li>
  <li>How to remove <code>float('nan')</code> from a set?</li>
  </ol>
  
  <p>Is the answer to always check before and never, ever add <code>float('nan')</code> to a set?</p>