What purpose does [0] serve in numpy.where(y_euler<0.0)[0]
<p>Also what is the difference between this:</p>

<pre><code>idx_negative_euler = numpy.where(y_euler&lt;0.0)[0]
</code></pre>

<p>and this:</p>

<pre><code>idx_negative_euler = numpy.where(y_euler&lt;0.0)[0][0]
</code></pre>

<p>I realize that this returns an array of indices where the array <code>y_euler</code> is negative, however I simply can't figure out what the <code>[0]</code> or the <code>[0][0]</code> at the end of the line is supposed to do.</p>

<p>I couldn't find any documentation regarding this (I'm not even sure what to search for). I've already looked into the <code>numpy.where</code> documentation but that didn't help.</p>

<p>Make a simple 1d array:</p>

<pre><code>In [60]: x=np.array([0,1,-1,2,-1,0])
</code></pre>

<p>Where returns a tuple <code>(...,)</code> of arrays, one for each dimension:</p>

<pre><code>In [61]: np.where(x&lt;0)
Out[61]: (array([2, 4], dtype=int32),)
</code></pre>

<p>pull the first (here only) element from the tuple</p>

<pre><code>In [62]: np.where(x&lt;0)[0]
Out[62]: array([2, 4], dtype=int32)
</code></pre>

<p>get the first element of the indexing array</p>

<pre><code>In [63]: np.where(x&lt;0)[0][0]
Out[63]: 2
</code></pre>

<p>the whole tuple returned by <code>where</code> can be used to index the array.  </p>

<pre><code>In [64]: x[np.where(x&lt;0)]
Out[64]: array([-1, -1])
</code></pre>

<p><code>x[2,4]</code>, <code>x[([2,4],)]</code> do the same indexing.</p>

<p>The usefulness of the <code>tuple</code> value becomes more obvious when working on a 2d or higher dim array.  In that case <code>np.where(...)[0]</code> would give the 'rows' index array.  But the <code>where(...)[0]</code> is most common in the 1d case where the tuple layer usually isn't needed.</p>

python arrays numpy