title: Fastest/correct way to iterate over NumPy array
body: <p>What would be the fastest way to iterate over an array? Using a <code>for</code> loop is known to be slower, such as:</p>
  
  <pre><code>&gt;&gt;&gt; for element in the_array: do thing
  </code></pre>
  
  <p>However, I have an array of size <code>(N, 7, 2)</code> and I need to do something with each element <code>N</code>. Sometimes this can be a vectorized calculation, so I know there is a fast solution there; sometimes its writing to file though. So my immediate solution is to iterate over a Python <code>range</code> and index the array:</p>
  
  <pre><code>&gt;&gt;&gt; for i in range(N): file.write(str(list(map(list, the_array[i]))))
  </code></pre>
  
  <p>Is there a faster way (or perhaps, a correct way if this is wrong) to iterate over the array?</p>