range() vs xrange() in Python

Last Updated : 5 Jun, 2026

The range() and xrange() are two functions that could be used to iterate a certain number of times in for loops. 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().

  • The Python range() function returns a sequence of numbers, in a given range. It is commonly used to iterate over a sequence of numbers in loops.
  • The xrange() function in Python is used to generate a sequence of numbers, similar to the Python range() function.

Return Type in range() vs xrange()

  • xrange() (Python 2): Returns an xrange object that generates values on demand using lazy evaluation, making it memory efficient.
  • range() (Python 3): Returns a range object, which also generates values lazily and provides memory-efficient iteration similar to Python 2's xrange().

Note: The following example is for Python 2, where both range() and xrange() are available.

Python
# initializing a with range()
a = range(1, 10000)

# initializing x with xrange()
x = xrange(1, 10000)
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'>

Speed of xrange() and range() Function

In Python 2, range() consumes more memory because it creates and stores the entire list of values, whereas xrange() returns an xrange object that generates values on demand.

Python
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

In Python 2, range() returns a list, so it supports list operations such as slicing, concatenation, and repetition. In contrast, xrange() returns an xrange object, which does not support all list operations.

Python
# 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])

Output: 

The list after slicing using range is :
[3, 4, 5]
The list after slicing using xrange is :
TypeError: sequence index must be integer, not 'slice'

Difference between range() and xrange() in Python

In Python 2, xrange() generates values on demand using lazy evaluation, making it more memory-efficient and generally faster than range() for large sequences.

Important Points: 

  • If you want to write code that will run on both Python 2 and Python 3, use range() as the xrange function is deprecated in Python 3.
  • xrange() has to reconstruct the integer object every time, but range() will have real integer objects. (It will always perform worse in terms of memory, however)

range()

xrange()

Returns a list of integers.Returns an xrange object.
Execution speed is slower.Execution speed is faster.
Takes more memory as it keeps the entire list of elements in memory.Takes less memory because values are generated on demand.
List operations can be performed on the result of range() in Python 2.Such operations cannot be performed on xrange().
In python 3, xrange() is not supported.In python 2, xrange() is used to iterate in for loops.
Comment