Setting values of an array without a loop
<p>I have arrays called <code>rad</code>, <code>relative_x</code>, <code>relative_y</code>, where <code>rad</code> is not of the same size as the other two. I want to create an array called <code>masking</code>, that will have the length of <code>rad</code> in the following way:</p>

<pre><code>masking[i] = (relative_x**2 + relative_y**2 &lt;= (rad[i])**2) &amp; (relative_x**2 + relative_y**2 &gt;= (rad[i]*0.9)**2)
</code></pre>

<p>but I want to do it without a loop. Is there a way to do this in python?</p>

<p><code>zip()</code> and a list comprehension pretty much does it. You still have a loop, but it's somewhat hidden.</p>

<pre><code>&gt;&gt;&gt; rad = [1.42, 4, 8]
&gt;&gt;&gt; relative_x = [1, 2, 3, 4, 5]
&gt;&gt;&gt; relative_y = [1, 2, 3, 4, 5]
&gt;&gt;&gt; 
&gt;&gt;&gt; masking = [rad_**2 &gt;= relative_x_**2 + relative_y_**2 &gt;= (rad_*0.9)**2
               for (rad_, relative_x_, relative_y_) in zip(rad, relative_x, relative_y)]
&gt;&gt;&gt; print masking
[True, False, False]
</code></pre>

<p>Note that in python, you're allowed to do <code>1 &lt; 2 &lt; 3</code>.</p>

python arrays