Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference b/w Python Range() vs Numpy.arange() function?

Tags:

python

numpy

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?

like image 451
AshishSingh007 Avatar asked May 03 '26 21:05

AshishSingh007


1 Answers

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.

like image 197
hpaulj Avatar answered May 06 '26 09:05

hpaulj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!