For large arrays, a vectorised numpy operation is the fastest. If you must loop, prefer xrange/range and avoid using np.arange.

In numpy you should use combinations of vectorized calculations, ufuncs and indexing to solve your problems as it runs at C speed. Looping over numpy arrays is inefficient compared to this.

(Something like the worst thing you could do would be to iterate over the array with an index created with range or np.arange as the first sentence in your question suggests, but I'm not sure if you really mean that.)

import numpy as np
import sys

sys.version
# out: '2.7.3rc2 (default, Mar 22 2012, 04:35:15) \n[GCC 4.6.3]'
np.version.version
# out: '1.6.2'

size = int(1E6)

%timeit for x in range(size): x ** 2
# out: 10 loops, best of 3: 136 ms per loop

%timeit for x in xrange(size): x ** 2
# out: 10 loops, best of 3: 88.9 ms per loop

# avoid this
%timeit for x in np.arange(size): x ** 2
#out: 1 loops, best of 3: 1.16 s per loop

# use this
%timeit np.arange(size) ** 2
#out: 100 loops, best of 3: 19.5 ms per loop

So for this case numpy is 4 times faster than using xrange if you do it right. Depending on your problem numpy can be much faster than a 4 or 5 times speed up.

The answers to this question explain some more advantages of using numpy arrays instead of python lists for large data sets.

Answer from bmu on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.3 Manual
>>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect ... Evenly spaced numbers with careful handling of endpoints. ... Arrays of evenly spaced numbers in N-dimensions. ... Grid-shaped arrays of evenly spaced numbers in N-dimensions. ... Try it in your browser! >>> import numpy as np >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.5.dev0 Manual
>>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect ... Evenly spaced numbers with careful handling of endpoints. ... Arrays of evenly spaced numbers in N-dimensions. ... Grid-shaped arrays of evenly spaced numbers in N-dimensions. ... Try it in your browser! >>> import numpy as np >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ arange
NumPy arange()
import numpy as np array = np.arange(1.0, 5.0, 0.5) print(array)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-arrange-in-python
numpy.arange() in Python - GeeksforGeeks
February 14, 2026 - numpy.arange() function creates an array of evenly spaced values within a given interval. It is similar to Python's built-in range() function but returns a NumPy array instead of a list.
Top answer
1 of 3
95

For large arrays, a vectorised numpy operation is the fastest. If you must loop, prefer xrange/range and avoid using np.arange.

In numpy you should use combinations of vectorized calculations, ufuncs and indexing to solve your problems as it runs at C speed. Looping over numpy arrays is inefficient compared to this.

(Something like the worst thing you could do would be to iterate over the array with an index created with range or np.arange as the first sentence in your question suggests, but I'm not sure if you really mean that.)

import numpy as np
import sys

sys.version
# out: '2.7.3rc2 (default, Mar 22 2012, 04:35:15) \n[GCC 4.6.3]'
np.version.version
# out: '1.6.2'

size = int(1E6)

%timeit for x in range(size): x ** 2
# out: 10 loops, best of 3: 136 ms per loop

%timeit for x in xrange(size): x ** 2
# out: 10 loops, best of 3: 88.9 ms per loop

# avoid this
%timeit for x in np.arange(size): x ** 2
#out: 1 loops, best of 3: 1.16 s per loop

# use this
%timeit np.arange(size) ** 2
#out: 100 loops, best of 3: 19.5 ms per loop

So for this case numpy is 4 times faster than using xrange if you do it right. Depending on your problem numpy can be much faster than a 4 or 5 times speed up.

The answers to this question explain some more advantages of using numpy arrays instead of python lists for large data sets.

2 of 3
13

First of all, as written by @bmu, you should use combinations of vectorized calculations, ufuncs and indexing. There are indeed some cases where explicit looping is required, but those are really rare.

If explicit loop is needed, with python 2.6 and 2.7, you should use xrange (see below). From what you say, in Python 3, range is the same as xrange (returns a generator). So maybe range is as good for you.

Now, you should try it yourself (using timeit: - here the ipython "magic function"):

%timeit for i in range(1000000): pass
[out] 10 loops, best of 3: 63.6 ms per loop

%timeit for i in np.arange(1000000): pass
[out] 10 loops, best of 3: 158 ms per loop

%timeit for i in xrange(1000000): pass
[out] 10 loops, best of 3: 23.4 ms per loop

Again, as mentioned above, most of the time it is possible to use numpy vector/array formula (or ufunc etc...) which run a c speed: much faster. This is what we could call "vector programming". It makes program easier to implement than C (and more readable) but almost as fast in the end.

๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ quickstart.html
NumPy quickstart โ€” NumPy v2.4 Manual
In complex cases, r_ and c_ are useful for creating arrays by stacking numbers along one axis. They allow the use of range literals :.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.ma.arange.html
numpy.ma.arange โ€” NumPy v2.1 Manual
>>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect ... Evenly spaced numbers with careful handling of endpoints. ... Arrays of evenly spaced numbers in N-dimensions. ... Grid-shaped arrays of evenly spaced numbers in N-dimensions. ... >>> import numpy as np >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])
๐ŸŒ
Medium
medium.com โ€บ @24littledino โ€บ range-python-vs-arange-numpy-3dc2953b9467
Range(Python) vs. Arange(NumPy). Introduction | by Little Dino | Medium
March 5, 2022 - Range(Python) vs. Arange(NumPy) Introduction range is a built-in function in Python, while arange is a function in NumPy package. They basically do the exact same thing. If you donโ€™t know how to โ€ฆ
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ how-to-partition.html
How to create arrays with regularly-spaced values โ€” NumPy v2.4 Manual
Both numpy.linspace and numpy.arange provide ways to partition an interval (a 1D domain) into equal-length subintervals.
๐ŸŒ
Real Python
realpython.com โ€บ how-to-use-numpy-arange
NumPy arange(): How to Use np.arange() โ€“ Real Python
July 31, 2023 - In this step-by-step tutorial, you'll learn how to use the NumPy arange() function, which is one of the routines for array creation based on numerical ranges. np.arange() returns arrays with evenly spaced values.
๐ŸŒ
H2K Infosys
h2kinfosys.com โ€บ blog โ€บ mastering range of numbers in array with numpy: arange & linspace simplified
Mastering Range of Numbers in Array with Numpy: arange & linspace Simplified
October 31, 2025 - Graphing and Visualization: Ranges are crucial for generating the x-axis values in plots and charts, facilitating data visualization. Using arange(): Generates evenly spaced values within a given interval. python import numpy as np # Creating ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ range and np.arange behave differently in foor loop
r/learnpython on Reddit: range and np.arange behave differently in foor loop
September 14, 2025 -

Hallo everyone,

I am trying to calculate the probability of an event.

When i use this code

import numpy as np
sum = 0
for i in np.arange(2, 1000, 2):
    sum = sum + (1/(2**i))
print(sum)

I get Inf

But when i use range instead of np.arange i get the right number which is 0.3333333

What difference dose range makes from np.arange in this case?

Thank you

Top answer
1 of 3
13
The np.arange() produces np.int64 objects, which have limited size. In contrast, the Python builtin range() produces int objects, which have arbitrary precision. Because of the limited precision of np.int64 values, the exponentiation can overflow, which results in a zero value here. That makes no sense mathematically, but this is about numerics, not pure math. Example with native Python ints: >>> 2**998 2678771517965668302371062622650004526403512029263834018609375970925877627812340306232995947039239645318986682293882867062967863214230785108996144393674643700983641943706057746355268651265592785469488545538261618745895485316849691889791385986519265728642799119421635541915107457913156096709301417017344 Example using Numpy fixed-size ints: >>> 2**np.int64(998) np.int64(0) To avoid this, use floats instead of integers for this exponentiation operation. For example: >>> 2.0**np.int64(998) np.float64(2.6787715179656683e+300) And by using a negative exponent instead of division, and using np.sum() instead of an explicit loop, we can express your calculation as: >>> np.sum(2.0**-np.arange(2,1000,2)) np.float64(0.3333333333333333)
2 of 3
5
When you use np.arange, you get a np.int64 as each i, which can't represent 2**64. (It can't correctly represent 2**63 either, come to that) So what happens is... In [10]: import numpy as np ...: sum = 0 ...: for i in np.arange(2, 1000, 2): ...: print(2**i) ...: sum = sum + (1/(2**i)) ...: print(sum) 4 16 64 256 1024 4096 16384 65536 262144 1048576 4194304 16777216 67108864 268435456 1073741824 4294967296 17179869184 68719476736 274877906944 1099511627776 4398046511104 17592186044416 70368744177664 281474976710656 1125899906842624 4503599627370496 18014398509481984 72057594037927936 288230376151711744 1152921504606846976 4611686018427387904 0 :5: RuntimeWarning: divide by zero encountered in scalar divide sum = sum + (1/(2**i))
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
arange() and linspace() to generate evenly spaced values
February 2, 2024 - The NumPy version used in this article is as follows. Note that functionality may vary between versions. ... np.arange() is similar to Python's built-in range() function.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.linspace.html
numpy.linspace โ€” NumPy v2.1 Manual
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, *, device=None)[source]#
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.4 Manual
>>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect ... Evenly spaced numbers with careful handling of endpoints. ... Arrays of evenly spaced numbers in N-dimensions. ... Grid-shaped arrays of evenly spaced numbers in N-dimensions. ... Try it in your browser! >>> import numpy as np >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.ma.arange.html
numpy.ma.arange โ€” NumPy v2.5.dev0 Manual
>>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect ... Evenly spaced numbers with careful handling of endpoints. ... Arrays of evenly spaced numbers in N-dimensions. ... Grid-shaped arrays of evenly spaced numbers in N-dimensions. ... Try it in your browser! >>> import numpy as np >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_array_from_numerical_ranges.htm
NumPy - Array From Numerical Ranges
In the following example, we are using the numpy.arange() function to generate an array starting from 0 up to (but not including) 10 โˆ’