Python xrange() function

The xrange() function in Python is used to generate a sequence of numbers, similar to the Python range() function. The Python xrange() is used only in Python 2.x whereas the range() function in Python is used in Python 3.x. 

Return Type in range() vs xrange()

This xrange() function returns the generator object that can be used to display numbers only by looping. The only particular range is displayed on demand and hence called “lazy evaluation“, whereas, in Python range() function returns a range object (a type of iterable).

Python3




# initializing a with range()
a = range(1, 10000)
 
# initializing a with xrange()
x = xrange(1, 10000)
 
# testing the type of a
print("The return type of range() is : ")
print(type(a))
 
# testing the type of x
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'>

Speed of xrange() and range() Function

The variable storing the range created by range() takes more memory as compared to the variable storing the range using xrange(). The basic reason for this is the return type of range() is list and xrange() is xrange() object. 

Python3




import sys
 
# initializing a with range()
a = range(1,10000)
 
# initializing a with xrange()
x = xrange(1,10000)
 
# testing the size of a
# range() takes more memory
print ("The size allotted using range() is : ")
print (sys.getsizeof(a))
 
# testing the size of x
# xrange() takes less memory
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 of xrange() and range() Function

A range() returns the list, all the operations that can be applied on the list can be used on it. On the other hand, as xrange() returns the xrange object, operations associated with the list cannot be applied to them, hence a disadvantage.

Python3




# initializing a with range()
a = range(1,6)
 
# initializing a with xrange()
x = xrange(1,6)
 
# testing usage of slice operation on range()
# prints without error
print ("The list after slicing using range is : ")
print (a[2:5])
 
# testing usage of slice operation on xrange()
# raises error
print ("The list after slicing using xrange is : ")
print (x[2:5])


Error: 

Traceback (most recent call last):
File "1f2d94c59aea6aed795b05a19e44474d.py", line 18, 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 :

range() vs xrange() in Python

The range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python. In Python3, there is no xrange, but the range function behaves like xrange in Python2. If you want to write code that will run on both Python2 and Python3, you should use range(). Both are implemented in different ways and have different characteristics associated with them. The points of comparison are:

  • Return Type
  • Memory
  • Operation Usage
  • Speed

Similar Reads

Python range() function

The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops....

Python xrange() function

The xrange() function in Python is used to generate a sequence of numbers, similar to the Python range() function. The Python xrange() is used only in Python 2.x whereas the range() function in Python is used in Python 3.x....

Difference between range() and xrange() in Python

...