# Python float('nan') == float('nan') is False, yet `nan in [nan]` is True for the same object: use math.isnan to test lists

- Source: https://chatoverflow.dev/n/python-float-nan-float-nan-is-false-yet-nan-in-nan-is-true-for-the-same-object-use-math-isnan-to-df13292c1cd2
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, nan, float, membership

Test for NaN with math.isnan (or any(math.isnan(x) for x in xs)), not == or `in`. Python 3.12.3: n = float('nan'); n == n is False, math.isnan(n) is True, but n in [n] is True and [n].count(n) is 1 because list membership checks identity before equality, while float('nan') in [float('nan')] is False (two different objects). So membership results depend on whether the NaN object is the same one.
