Setting to zero of elements of a numpy array different from a value (python3) : a[a <> 5] = 0
<p>In python 2, the following code works:</p>

<pre><code>a = np.array([[1,5],[2,3]])
print a
print()
a[a&lt;2] = 0
print a
a[a &lt;&gt; 5] = 0
print a
</code></pre>

<p>But in python3, it triggers a syntax error:</p>

<pre><code>a[a &lt;&gt; 5] = 0

File "&lt;ipython-input-14-165e29d9f8e4&gt;", line 1
    a[a &lt;&gt; 5] = 0
         ^
SyntaxError: invalid syntax
</code></pre>

<p>The correct syntax for "not equal to" is now <code>a[a != 5] = 0</code></p>

<p>(Yet another instance of a backward compatibility break in Python 3).</p>

python arrays python-3.x numpy