You should add `from extern.six.moves import range` at the top so that this `range` has `xrange` semantics on all Pythons, otherwise this will actually create 2 temporary lists on Python 2.