Replacing a for-in loop by an index loop on an array

This fixes ipython/ipython#4686.