range() vs xrange() in Python with examples
Hey there, fellow Python programmers, this tutorial talks about range() vs xrange() in Python with examples.
So, let’s get started…
The first question that comes to our mind is what is range() or xrange() in Python?
-> Well both range and xrange are used to iterate in a for loop.
-> But the difference lies in what the return:
- range(): It returns a list of values or objects that are iterable.
- xrange(): It returns an object which acts as a generator because it doesn’t store any values like range rather it generates them when its queried or used.
This difference in which both works leads to various implementation differences…
(NOTE: Python 3.x doesn’t support xrange rather range has taken its place and now to get a list of values we need to explicitly ask for them like a=list[range(1,100)].)
The range() and xrange() functions can be compared based on:
- Memory Usage
- Computational speed
- Usage ways
- Return Type
Well talking about memory usage then range() occupies much more memory as compared to xrange().
This is because range() function compiles the whole list of numbers beforehand and stores it in the memory.
While xrange() compiles the object when it is asked to do so.
So, in the case of xrange() it doesn’t occupy memory until its iterated.
Let’s look through this with the help of an example:
import sys a =range(1,50000) x =xrange(1,50000) print ("Memory allocated by range() function : ") print (sys.getsizeof(a)) print ("Memory allocated by xrange() function : ") print (sys.getsizeof(x))
We have imported the “sys” library to use the “getsizeof” function in python.
Let’s see how the output looks:
That’s a lot of memory in use.
Now, let’s move onto something else.
Well, talking about the computational speed the xrange() function takes lesser time to execute than the range() function.
This is because the xrange() function returns a generator hence which gives out values when we iter.
On the other hand, the range() function stores all values at once.
Let’s see this difference with the help of a code:
import time start_var = time.time() #initializing a variable with range() var = range(1,10000) end_var = time.time() time_var=str(end_var-start_var) print("Time taken for range() :" + time_var) start_x = time.time() #initializing a variable with xrange() x_var= xrange(1,10000) end_x = time.time() time_x= str(end_x - start_x) print("Time taken for xrange() :" + time_x)
Here, we have imported the “time” library to measure the interval of execution.
Let’s look at the output:
Here, you could notice that there is a large difference between the compilation times of both.
NOTE: If we are to iter over a list again and again then range() is a better option because it has all its values pre-assigned so we just need to iter over it on the other hand xrange() assign values each time we iter over it hence it takes more time.
Well taking about the usage, all operations that can be applied on a list can be applied on the range() function.
But, this is not so for the case of xrange() because it returns an object, not a list.
Let’s have a look over this with the help of code:
#Trying slicing on range() and xrange() function. #slicing on range() var = range(1, 19, 3) print ("List (range()) after slicing operation :") print (var[3:7]) #slicing on xrange() x_var = xrange(1, 19, 3) print ("List (xrange()) after slicing operation :") print (x_var[3:7])
Now let’s look at the output of this:
The xrange() function throws up an error because it does not allow list operations.
The range() function returns a list i.e its type is list.
The xrange() function returns a xrange() object.
Let’s see this difference with the help of code:
#Code to check the return type. #Range() function variable var = range(1,5000) #Xrange() function variable var_x= xrange(1,5000) # return type of range() variable print ("The return type of range() function is : ") print (type(var)) # return type of xrange() variable print ("The return type of xrange() function is : ") print (type(var_x))
Now, let us look at the output result:
You can see the difference.
And there you have it “range() vs xrange() in Python with examples”.
I hope you loved the read.
Thanks for reading.