Why in numpy `nan == nan` is False while nan in [nan] is True? Why in numpy `nan == nan` is False while nan in [nan] is True? numpy numpy

Why in numpy `nan == nan` is False while nan in [nan] is True?


nan not being equal to nan is part of the definition of nan, so that part's easy.

As for nan in [nan] being True, that's because identity is tested before equality for containment in lists. You're comparing the same two objects.

If you tried the same thing with two different nans, you'd get False:

>>> nans = [float("nan") for i in range(2)]>>> map(id, nans)[190459300, 190459284]>>> nans[nan, nan]>>> nans[0] is nans[1]False>>> nans[0] in nansTrue>>> nans[0] in nans[1:]False

Your addendum doesn't really have much to do with nan, that's simply how Python works. Once you understand that float("nan") is under no obligation to return some nan singleton, and that y = x doesn't make a copy of x but instead binds the name y to the object named by x, there's nothing left to get.