range () versus xrange () in python



range () — returns a list of numbers created with the range () function. 
xrange () — this function returns a generator object which can be used to display numbers only by looping. When requested, only a specific range is displayed, and therefore it is called “ deferred .

Both are implemented differently and have different characteristics associated with them. Comparison points:

  • Return type
  • Memory
  • Operation Usage
  • Speed ​​

Return type

range () returns —  list as a return type. 
xrange () returns — object xrange () .

# Python code to demonstrate range () and xrange ()
# based on return type

 
# initialization with range ()

a = range ( 1 , 10000 )

 
# initialization with xrange ()

x = xrange ( 1 , 10000 )

 
# type testing

print ( "The return type of range () is : " )

print ( type (a))

  
# type x testing

print ( "The return type of xrange () is:" )

print ( type (x))

Output:

 The return type of range () is: The return type of xrange () is: 

Memory

A variable storing a range created by range () takes more memory compared to a variable storing a range using xrange (). The main reason for this is the return type of range () — list, and xrange () — the xrange () object.

# Python code to demonstrate range () and xrange ()
# memory-based

  

import sys

 
# initialization with range ()

a = range ( 1 , 10000 )

 
# initialization with xrange ()

x = xrange ( < code class = "value"> 1 , 10000 )

 
# testing the size
# range () takes more memory

print ( "The size allotted using range () is:" )

print (sys.getsizeof (a))

 
# testing the size
# range () 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  

Using operations

Since range () returns a list, all operations that can be applied to a list can be used with it. On the other hand, since xrange () returns an xrange object, list-related operations cannot be applied to them, which is a drawback.

# Python code to demonstrate range () and xrange ()
# based on usage

 
# initialization with range ()

a = range ( 1 , 6 )

 
# initialization with xrange ()

x = xrange ( 1 , 6 )

  
# testing the use of the slice operation in range ()
# prints without errors

print ( "The list after slicing using range is:" )

print (a [ 2 : 5 ])

 
# testing using the slice operation in xrange ()
# gives an 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: 

speed

Due to the fact that xrange () only evaluates a generator object containing only the values ​​required for lazy evaluation, therefore is faster in implementation than range ().

Important points:

  • If you want to write code that works in both Python 2 and Python 3, use range () as xrange is deprecated in Python 3
  • range () is faster if you repeat the same sequence multiple times.
  • xrange () should restore each time an integer object, but range () will have real integer objects. (This will always work worse in terms of memory, however)

This article is provided by Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.