Nameerror: name ‘xrange’ is not defined

How to fix the Python nameerror: name ‘xrange’ is not defined error message?

In this article, we’ll hand you the solutions to this python xrange is not defined.

If this error gives you a hard time, then continue reading. But before we proceed to the solution.

Let’s first understand what this error is all about and what are the reasons why it occurs in your Python script.

What is xrange()?

The xrange() is a built-in function in Python version 2 that generates a sequence of integers.

It is similar to the range() function however, unlike range(), which generates the entire sequence at once and stores it in memory.

Xrange() generates the numbers one at a time as you iterate over the sequence. This can be more memory-efficient if you are working with large sequences.

What is “nameerror: name ‘xrange’ is not defined”?

The error message nameerror: name ‘xrange’ is not defined raised when you’re trying to access the xrange() function in Python version 3.

Unfortunately, xrange() was removed in Python version 3.

If you are working with Python version 3, use the range() function instead. The range() function in Python version 3 acts like the xrange() function in Python version 2.

For example:

for x in range(5):
    print(x)

If we try to run this code it will result to:

Traceback (most recent call last):
  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1, in <module>
    for x in xrange(5):
             ^^^^^^
NameError: name 'xrange' is not defined. Did you mean: 'range'?

Moreover, a nameerror is a common type of error that occurs in Python when the interpreter cannot find a variable or function with a particular name.

In this nameerror name ‘xrange’ is not defined it indicates that the interpreter is unable to find the xrange() function.

Why does this error occur?

This name xrange is not defined can occur if you are running a code in Python version 3 that was written for Python version 2 without making the necessary changes to update it for the new version of the language.

How to fix “nameerror: name ‘xrange’ is not defined”?

To fix the nameerror name ‘xrange’ is not defined, you have to replace xrange() function in your code with the range() function.

1. Replace xrange() with range()

As we mentioned above, the xrange() function has been removed and replaced by the range() function, which acts like xrange() in Python version 2.

Example code for Python version 2:

for x in xrange(5):
    print(x)

This code will result to an error message if you use this in Python version 3.

Try to use the following code for Python 3.

Updated code for Python version 3:

for x in range(5):
    print(x)

Output:

0
1
2
3
4

2. Define a custom xrange() function

When you need to use the code that uses xrange(), it might be hard to do it manually to replace every xrange() with range().

If that’s the case, you can define a custom function that returns range(n) when called with an argument n.

def xrange(n):
    return range(n)

for x in xrange(5):
    print(x)

In this example code, we’ve defined a custom function xrange(n) that returns range(n), and then we’ve used this custom function in the loop instead of xrange().

Output:

0
1
2
3
4

3. Import xrange() from six.moves module

When you’re working with third-party libraries or code that still uses xrange(), you can import the xrange() function from the six.moves module, which provides a compatibility layer for using Python version 2 code in Python version 3.

For example:

from six.moves import xrange

for x in xrange(5):
    print(x)

Output:

0
1
2
3
4

Tips to avoid this error

To avoid the nameerror: name ‘xrange’ is not defined error, it’s best to write Python version 3 compatible code. Here are some tips to help you avoid this error:

✅ Use the range() function instead of xrange().

✅ Ensure you’re using the correct version of Python.

✅ If you need to use xrange() for compatibility reasons, you can refer to the above solutions.

✅ Use a code analyzer or linter to check your code for compatibility issues.

Conclusion

In conclusion, the error message nameerror: name ‘xrange’ is not defined raised when you’re trying to access the xrange() function in Python version 3.

To fix the nameerror name ‘xrange’ is not defined, you have to replace xrange() function in your code with the range() function.

This article already provides solutions for this error to help you fix the Python error message.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊