Difference Between range and xrange in Python | xrange vs range
Python list is one of the very important data structure. To generate the list or sequence of elements, Python version 2 has two inbuilt handy functions called
Whereas, xrange is deprecated from Python version 3.
Before going into the difference between range and xrange, let’s see what it is.
The function range and xrange generated the series of numbers. It takes 3 arguments- start, stop and step.
- Start is the first integer value generated in the range.
- Stop is the last integer value where you would like to stop iterating.
- Step is integer value by which each element is incremented (default value is 1).
All the three arguments belong to the numeric data type of Python.
Here is an example using range() and xrange() for iterating over the loop and printing values from 1 to 9 (9 inclusive).
for x in range(1,10,1): print x
for x in xrange(1,10,1): print x
For both codes, the output is same.
0 1 2 3 4 5 6 7 8 9
Both work exactly same functionally and gives the same output.
What is the difference between range and xrange?
Even though the range and xrange give the same output, each element is evaluated differently during each iteration.
Let’s figure out…
- How are they different from each other?
- Which one does take more memory?
- Which one is faster to execute (performance)?
- Why is xrange deprecated from Python 3.x?
xrange vs range | Working Functionality:
The range is an inbuilt function that generates the list. It creates a static list before running the iteration.
Let’s take an example of
Note: We are only passing two parameters. By default step-parameter value is 1. So, while each iteration, the value is incremented by one.
It creates the list of size 9 having elements 1 to 9 (9 inclusive) and returns list object.
rObj = range(1, 10) print rObj #[1,2,3,4,5,6,7,8,9] type(rObj) #<type 'list'>
Related Read: 5 Major Difference Between List and Tuple in Python.
Unlike range, xrange returns xrange sequence object.
xObj = xrange(1, 10) print xObj #xrange(1, 10) print type(xObj) #<type, 'xrange'>
It doesn’t create a list and each sequence element is evaluated lazily during each iteration. In other words, next iteration is similar to the returning last_value+1.
Taking an example of
xrange(1, 10) …
It evaluates each element in the sequence and returns values from 1 to 9 (9 inclusive).
The xrange() is an another generator and the value is created using a special technique called as yielding. For more detail, you can read generator function and yield value in Python.
Which does take more memory?
As range() created a list of all the elements and save it before iterating. Whereas, xrange does not saves any element and evaluate each element during each iteration.
So, range() takes more memory to save these elements in the list.
Below is code sample for testing. Generate the gigantic number of elements using both range and xrange. The range() will throw the MemoryError exception.
>>> xrange(int(1e15)) xrange(1000000000000000) >>> >>> >>> range(int(1e15)) Traceback (most recent call last): File "", line 1, in MemoryError >>>
It is clear, if you have limited memory space to run your program where you need to generate a gigantic range of elements, you should use xrange() over the range().
Which is faster?
Running xrange() is slower as compared to the range() as xrange evaluates the value of element during each iteration. It is also called as lazy evaluation.
You can use timeit module to figure out the time required for each iteration during evaluating values for range() and xrange().
$ python -m timeit 'for i in range(1,1000000):' ' pass' 10 loops, best of 3: 28.2 msec per loop $ python -m timeit 'for i in xrange(1,1000000):' ' pass' 100 loops, best of 3: 9.87 msec per loop
If you observe, the time required to generate element using xrange is higher than using range. The
range() is faster than
Which one to use when?
If you are performing an action on very large range elements, it is good to use xrange(). It doesn’t save any elements. It just evaluates next value whenever needed.
xrange() is very well optimized than
If you need faster execution of your program, use range().
Deprecation of xrange() for Python 3.x
In Python 2.x, the xrange() require very less memory as it doesn’t save any sequence values. Even though xrange takes more time for iteration, the performance impact is very negligible.
So, they wanted to use xrange() by deprecating range(). In Python 3.x, they removed range (from Python 2.x) and renamed xrange() as a range().
Using a range with different Python Version…
- In Python 3.x, xrange() is removed and there is an only range()
- range() in Python 3.x works exactly same as xrange() in Python 2.x
To make the code portable so that it should work with both Python version, use
range() instead of
Some of the Examples of range and xrange:
Example 1: Printing all the values in the range from 0 to 9.
for i in range(10): print(i)
for i in xrange(10): print(i)
0 1 2 3 4 5 6 7 8 9
In the above example, we are only passing a single parameter (stop) to the functions
xrange(). The default value for parameters – start and step are zero and one respectively.
Example 2: Print all the odd numbers between 1 and 10.
for i in range(1, 10, 2): print(i)
for i in xrange(1, 10, 2): print(i)
1 3 5 7 9
It also works with negative values.
Example 3: Python program to print values from -1 to -9 using range and xrange.
for i in range(-1, -10, -1): print(i)
for i in xrange(-1, -10, -1): print(i)
-1 -2 -3 -4 -5 -6 -7 -8 -9
If you are new to this portal, we have complete Python tutorial available for FREE. Let’s start learning Python.
This is all about the difference between range and xrange in Python. Hope it clears your doubt so that you are able to make the decision which one to use. If you have any query, feel free to write in the comment.