3

I learned on my web search that numpy.arange take less space than python range function. but i tried using below it gives me different result.

import sys

x = range(1,10000)
print(sys.getsizeof(x))  # --> Output is 48

a = np.arange(1,10000,1,dtype=np.int8)
print(sys.getsizeof(a))  # --> OutPut is 10095

Could anyone please explain?

David
  • 8,113
  • 2
  • 17
  • 36

3 Answers3

3

In PY3, range is an object that can generate a sequence of numbers; it is not the actual sequence. You may need to brush up on some basic Python reading, paying attention to things like lists and generators, and their differences.

In [359]: x = range(3)                                                                                 
In [360]: x                                                                                            
Out[360]: range(0, 3)

We have use something like list or a list comprehension to actually create those numbers:

In [361]: list(x)                                                                                      
Out[361]: [0, 1, 2]
In [362]: [i for i in x]                                                                        
Out[362]: [0, 1, 2]

A range is often used in a for i in range(3): print(i) kind of loop.

arange is a numpy function that produces a numpy array:

In [363]: arr = np.arange(3)                                                                           
In [364]: arr                                                                                          
Out[364]: array([0, 1, 2])

We can iterate on such an array, but it is slower than [362]:

In [365]: [i for i in arr]                                                                             
Out[365]: [0, 1, 2]

But for doing things math, the array is much better:

In [366]: arr * 10                                                                                     
Out[366]: array([ 0, 10, 20])

The array can also be created from the list [361] (and for compatibility with earlier Py2 usage from the range itself):

In [376]: np.array(list(x))     # np.array(x)                                                                        
Out[376]: array([0, 1, 2])

But this is slower than using arange directly (that's an implementation detail).

Despite the similarity in names, these shouldn't be seen as simple alternatives. Use range in basic Python constructs such as for loop and comprehension. Use arange when you need an array.

An important innovation in Python (compared to earlier languages) is that we could iterate directly on a list. We didn't have to step through indices. And if we needed indices along with with values we could use enumerate:

In [378]: alist = ['a','b','c']                                                                        
In [379]: for i in range(3): print(alist[i])   # index iteration                                                        
a
b
c
In [380]: for v in alist: print(v)    # iterate on list directly                                       
a
b
c
In [381]: for i,v in enumerate(alist): print(i,v)    #  index and values                                                  
0 a
1 b
2 c

Thus you might not see range used that much in basic Python code.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Very useful deep dive. Perhaps also compare `np.array(10*list(range(5)))` with `10*np.array(list(range(5)))` to show how to correctly create an _array-with-math_ from a pre-existing list. – P2000 Sep 22 '20 at 17:24
1

the range type constructor creates range objects, which represent sequences of integers with a start, stop, and step in a space efficient manner, calculating the values on the fly.

np.arange function returns a numpy.ndarray object, which is essentially a wrapper around a primitive array. This is a fast and relatively compact representation, compared to if you created a python list, so list(range(N)), but range objects are more space efficient, and indeed, take constant space, so for all practical purposes, range(a) is the same size as range(b) for any integers a, b

As an aside, you should take care interpreting the results of sys.getsizeof, you must understand what it is doing. So do not naively compare the size of Python lists and numpy.ndarray, for example.

Perhaps whatever you read was referring to Python 2, where range returned a list. List objects do require more space than numpy.ndarray objects, generally.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
1

arange store each individual value of the array while range store only 3 values (start, stop and step). That's the reason arange is taking more space compared to range. As the question is about the size, this will be the answer.

But there are many advantages of using numpy array and arange than python lists for speed, space and efficiency perspective.

Emma
  • 27,428
  • 11
  • 44
  • 69
Lin Lae
  • 96
  • 4