In Python, Both range() and xrange() functions produce the integer numbers between a given start value and stop value. So What is the difference between range() and xrange() functions? Let’s check it out.
Python xrange() and range() function
In Python 2 we have the following two functions to produce a list of numbers within a given range.
in Python 3, xrange() is deprecated, i.e. xrange() is removed from python 3.x.
But, range() function of python 3 works same as
xrange() of python 2 (i.e. internal implementation of
range() function of python 3 is same as
xrange() of Python 2).
So The difference between
xrange() functions becomes relevant only when you are using python 2.
Difference between range() and xrange() functions in Python
There are many differences between these two functions concerning performance, memory, speed and internal implementation. Both implemented in different ways. Let see this one by one.
range() and xrange() Functionality
In terms of functionality both range() and xrange() functions generates integer numbers within given range. i.e,.
range (1,5,1) will produce [1,2,3,4] and
xrange(1,5,1) will produce
[1,2,3,4]. So no difference in terms of functionality between
range() and xrange() return value
- range() creates a list i.e., range returns a Python list object, for example,
range (1,500,1)will create a python list of 499 integers in memory. Remember, range() generates all numbers at once.
- xrange() functions returns an xrange object that evaluates lazily. That means xrange only stores the range arguments and generates the numbers on demand. It doesn’t generate all numbers at once like range(). Furthermore, this object only supports indexing, iteration, and the
On the other hand xrange() generates the numbers on demand. That means it produces number one by one as for loop moves to the next number. In every iteration of for loop, it generates the next number and assigns it to the iterator variable of for loop.
range() and xrange() return type
- range() return type is Python list.
- xrange() function returns xrange object.
Printing return type of range() function:
range_numbers = range(2,10,2) print ("The return type of range() is : ") print (type(range_numbers ))
The return type of range() is : <type 'list'>
Printing return type of xrange() function:
xrange_numbers = xrange(1,10,1) print "The return type of xrange() is : " print type(xrange_numbers )
The return type of xrange() is : <type 'xrange'>
range() vs xrange() performance and speed test
We tested the speed of both the functions with a
timeit module to test which code is faster. Let’s see the result. In our speed test, we found xrange() is faster than range.
Speed test with Python range() function
$ python -m timeit 'for num in range(1000000):' ' pass'
We executed above loop 5 times and found that average time per loop with range() is 92.8 millisecond. Speed test with Python xrange() function
python -m timeit 'for i in xrange(1000000):' ' pass'
We executed above loop 5 times and found average time per loop with xrange() is 52.3 millisecond. So it’s clear now xrange is faster and saves memory.
- As you know,
xrangedoesn’t generate a static list at run-time as the range does. xrange() creates the values on-demand as you need them using a technique called yielding.
range ()function produced all numbers instantaneously before the for loop started executing.
The main problem with the original
range() function is it uses a considerable amount of memory if you are producing a vast range of numbers.
xrange() function always produces the next number on the fly, i.e. only keeps one number in memory at a time to consume less memory.
Should we always favor xrange over a range()
The advantage of the xrange function is it always takes the same amount of memory, no matter the size of the range it represents. There are consistent performance advantages. Let see the scenarios where we can use range and xrange.
Scenarios where we can use xrange() function
- If you are dealing with an extensive range of numbers, then I recommend you to use xrange(), i.e. you should always favor xrange() over a range() if we want to generate the vast range of numbers.
- xrange() is very helpful if your system has less memory like cell phones. Using xrange(), you can prevent memory overflow errors.
- As xrange() resolves each number lazily, if you stop iteration early, you won’t waste time creating the whole list.
Scenarios where we should use range() function
- If you want to iterate over the list multiple times, then I recommend you to use range(). Because xrange has to generate an integer object whenever we access an index, this can overhead.
- If you need to generate fewer numbers, then go for
range ()returns the list so that you can use all list operations on it. On the other hand, you know xrange() returns the xrange object, so you can’t use all list operations on it.
- Compatibility of code – If you want to write code that must run on both Python 2 and Python 3, use
range()as the xrange function removed from Python 3.
Python xrange() not defined error
You will get “python xrange not defined” error if you try to use xrange() in Python 3, xrange() is removed from Python 3. If you try to import xrange() function in Python, you encountered an error. You will get this error NameError: global name ‘xrange’ is not defined in Python 3. better to use range().
To practice what you learned in this article, I have created a Python Basic Quiz and Exercise.
Let me know your comments and feedback in the section below.