How To Solve “NameError: name ‘xrange’ is not defined” In Python

NameError: name ‘xrange’ is not defined in Python

“NameError: name ‘xrange’ is not defined” error in Python is a popular error related to the ‘xrange’ function. You can read the explanations below to have more knowledge about the cause of this error and solutions.

How does the error “NameError: name ‘xrange’ is not defined” in Python happen?

This error happens due to the many following reasons:

Firstly, it might be because you are working on Python 3. In Python 3, there is no ‘xrange’ function anymore. Trying to use the ‘xrange’ function in Python3 will result in the error “NameError: name ‘xrange’ is not defined”.

Second, if you are using Python 2.x and this error occurs, there might be some mistakes when you type the command to run Python or type the ‘xrange’ keyword such as ‘Xrange’ or ‘xRange’.

How to solve the error?

Solution 1: Using range() instead

In a Python for loop, we may iterate several times by using the methods range() and xrange(). The xrange() is not a function in Python 3. However, the range() function functions similarly to xrange() in Python 2. Use the range() method when creating code that will work with Python 2 and Python 3:

for i in range(1, 4): # Instead of xrange(1, 4)

Both are put into practice in various ways and have various pros and cons attached to them. The comparative criteria are:

  • Return Type
  • Memory
  • Speed

range(): This function returns a range object (an iterable type).

xrange(): This function returns a generator object that can be used to display numbers just by looping. Only the specific range is displayed on request and is therefore called lazy evaluation.

Solution 2: Checking spelling error

Remember that in Python, ‘range’ is a function, not a const or a name. If you want to use ‘xrange’ or ‘range’ you will need to put round brackets around it to specify the parameters. For example:

range(start, stop)
xrange(start, stop)
range(start, stop, step)
xrange(start, stop, step)

And one point to remember, you need to check again if you have written the correct keyword ‘xrange’ (or ‘range’) or not. There may be you have type such as these incorrect examples:

Xrange(0, 1)
Range(2, 4)

The range storage variable created by the range() function takes up more memory than the range storage variable using xrange(). Because the return type of xrange() is the ‘xrange’ object while that of range() is ‘list’. Since range() returns a list, all the methods and operations that can be used in the ‘list’ can work on it. On the contrary, since xrange() returns an ‘xrange’ object, methods and operations related to ‘list’ cannot be used on it, which is a disadvantage.

x = range(1, 4)
y = xrange(1, 4)
print(y[0]) # Get error because y is not a list


We have learned how to handle the error “NameError: name ‘xrange’ is not defined” in Python. By checking the version of the current Python carefully and replacing it using ‘xrange’ with ‘range’ as guided in this tutorial, you can solve it easily.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *