Python's range() vs xrange() Functions
Both range() and xrange() are built-in functions in Python that are used to generate integers or whole numbers in a given range . The range() and xrange() comparison is relevant only if you are using both Python 2.x and Python 3 . It is because the range() function in python 3.x is just a re-implementation of the xrange() of python 2.x. It actually works the same way as the xrange does.
In Python 2.x:
- range() creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements. This will become an expensive operation on very large ranges.
- xrange() is a sequence object that evaluates lazily. It is much more optimised, it will only compute the next value when needed (via an xrange sequence object) and does not create a list of all values like range() does.
In Python 3:
- range() does the equivalent of python's xrange(), and to get the list. If you need to actually generate the list, you will need to do:
The advantage of the range() type is that it is faster if iterating over the same sequence multiple times. On the other hand the 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). Also, xrange() isn't usable in all cases where a real list is needed. For instance, it doesn't support slices , or any list methods.
The advantage of the xrange() type is that an xrange() object will always take the same amount of memory, no matter the size of the range it represents. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. For performance, especially when you're iterating over a large range, xrange() is usually better.
Syntax of range and xrange:
- range() – This returns a list of numbers created using range() function.
- xrange() – This function returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called lazy evaluation .
NameError: name 'xrange' is not defined
The error message 'xrange' is not defined would seem to indicate that you are trying to run a Python 2 codebase with Python 3 . In Python 2, an iterable object is often created with xrange() method , usually in a "for loop", which behaves very similarly to a generator. In Python 3, range() method is implemented the same as the xrange() method , so there is no dedicated xrange(). So, if you use xrange() in Python 3, it will generate NameError:name 'xrange' is not defined error.