Difference between range() and xrange() in Python

by | Jan 11, 2023 | Python

Home » Python » Difference between range() and xrange() in Python

Introduction

This article illustrates the Difference between range() and xrange() in Python . Both the range() and the xrange() functions are used to determine the number of iterations that a loop will undergo. Especially in for loops, these methods are used to determine the range of iteration of the loops. The xrange() method is not present in Python 3. The range() function in Python 2 has a similar functionality to that of the xrange function(). Range() is common to both Python 2 and Python 3. Therefore, if you are planning to write universal code that will run in both these Python versions, then it is advisable to use range().

range()

The range() function will return an iterable range object that will tell the system the range of the for loop.

xrange()

The xrange() function will return the generator object. This object will only print out the number (range) by iterations. You will have to additionally request the particular range that you are looking for. Therefore this function is often termed as lazy evaluation.

Even though both these functions look alike and might have the same configuration, the characteristics and ways of implementation are different. We will be comparing these functions on these grounds:

  • Return Type
  • Memory
  • Operation Usage
  • Speed

Return Type

The range() function returns a range object and the xrange() function returns an xrange() object.

#The following Python code will help understand the difference between the two functions with #respect to their return types.

#creating a new variable and initializing a range on it
a = range (1, 10000)

#creating a new variable and initializing an xrange on it
x = xrange (1, 10000)

#Now checking the return type of both the range() and the xrange() functions
print (“The return type of range() is :”)
print (type (a))

print (“The return type of xrange() is :”)
print (type (x))

OUTPUT

The return type of range() is:
<type ‘list’>
The return type of xrange() is:
<type ‘xrange’>

Memory

When talking about the differences between the two functions, range() and xrange() in terms of memory, we find out that he variable that stores range using the range() function actually occupies more memory than the variable that stores xrange using the xrange() function. A simple explanation for this could be the fact that the return type of range is actually a list and the return type of xrange is an object. List occupies more space as compared to an object thus this difference in space occupied.

#Python code to show the difference between range() and xrange() with respect to memory

import sys

#creating a new variable and initializing a range on it
a = range (1, 10000)

#creating a new variable and initializing an xrange on it
x = xrange (1, 10000)

#using the getsizeof() function on a and x to check which one occupies more space.
#it is expected that the variable storing range, a in this case, will occupy more space as compared to #x
print (“The size allotted using range() is :”)
print (sys.getsizeof (a))

print (“The size allotted using xrange() is :”)
print (sys.getsizeof (x))

OUTPUT

The size allotted using range() is :
80064

The size allotted using xrange() is:
40

Operations Usage

In this classification, we will look at the operations that can be applied to range() and xrange(). As mentioned before, the return type of the range() function is a list. Therefore, we can apply all those functions and operations to it that can be applied to a list. In simpler words, we can treat the range() function return type to a list. A major disadvantage here is that the return type of xrange() is an object thus the functionalities of a list cannot be applied here. This actually limits the amount of functionality that can be associated with xrange.

#Python code to show the difference between range() and xrange() with respect to operations usage

#creating a new variable and initializing a range on it
a = range (1, 6)

#creating a new variable and initializing an xrange on it
x = xrange (1, 6)

#Implementing the slice operation here which is a key operation of a list. In the case of range(), we #will get output without any error. However, when the slice operation is implemented on xrange #variable, an error will be displayed.

print (“The list after slicing using range is :”)
print (a [2:5])

print (“The list after slicing using xrange is :”)
print (x [2:5])

Error:
Traceback (most recent call last):
File “1f2640dhunk3009dnu779snm4872 abmm3.py”, line 198, in print (x [2:5])
TypeError: sequence index must be integer, not ‘slice’

OUTPUT

The list after slicing using range is :
[3, 4, 5] The list after slicing using xrange is:

Speed

Finally, we will be comparing range() and xrange() on the basis of their speed of execution. Since xrange() creates a generator object, these objects have only those value that will be required during lazy initialization. Thus xrange() is several times faster than range() when it comes to execution of the two functions.

Important Notes

  • As mentioned before, it is very important to remember that xrange() is not supported in Python. Therefore, if you want your code to run universally, always use range().
  • If the sequence or collection over which you want to iterate or traverse does not change over time, then range() gives you faster results as compared to xrange().
  • When using xrange(), the function will have to take value from the generator object and reconstruct the integer object. But with range(), it automatically gives you real integer objects. However, it is important to keep in mind that more memory will be occupied when using range().

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author