Everything You Need to Know About Xrange Function in Python

xrange in python is a function that is used to generate a sequence of numbers from a given range. It is a function available in python 2 which returns an xrange object. The xrange function comes into use when we have to iterate over a loop. Thus, the object generated by xrange is used mainly for indexing and iterating.

Note: Xrange is only available in Python 2. As the support for Python 2 is officially dead, we suggest you use Python3 along with the range() function.

Syntax of xrange() in python

The syntax of xrange() is:

xrange(start, stop, step)

xrange takes three parameters – start, stop and step

  • start: It is the starting point from which the sequence would be generated. The start is an optional parameter, and if not mentioned, by default, its value is zero. The start value is inclusive, i.e., the number mentioned would be included in the sequence generated.
  • stop: It is the ending position of the sequence. The stop value is exclusive, i.e., the sequence generation would stop as soon as the number is obtained. Therefore, it would not be included in the sequence.
  • step: It is also an optional value that, if not mentioned, is one by default. Step size is the difference between two consecutive numbers in the sequence.

Using the xrange() function

We shall look at some different ways and examples of using the xrange() in python. Different combinations of using xrange() – only passing the stop argument, passing both starts and stop, passing all the arguments start, stop and step.

Passing only stop argument

Since the start and step arguments are optional, we can create an xrange() function by only passing the stop argument. By only passing one argument, python will take it as the stop value. And the start and step parameters would be initialized default values 0 and 1, respectively.

We shall consider an example to print 0 to 9 numbers. To do so, we shall pass 10 as the stop argument to the xrange() function. Since it is an exclusive value, numbers up to 10 would be generated.

n = xrange(10)

Now, since the type of n is an xrange object, we will have to convert it into a list explicitly to obtain the numbers. So now, we shall print that list.

print(list(n))

The output will be a list containing a sequence of numbers generated from 0 to (stop-1), i.e., from 0 to 9 in this case.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Passing both start and stop arguments

When we pass two arguments to the xrange() function, the two values are taken as start and stop values. In that case, the step parameter is assigned the default value 1.

We shall consider an example where we shall print values from 10 to 20. Here, the start value would be inclusive and assigned value 10. At the same time, the stop value would be exclusive and assigned the value 20.

We shall pass 10 and 20 as parameters to xrange(), where 10 would be passed first, followed by 20.

n = xrange(10,20)

Now, we shall convert n into list and print that list.

print(list(n))

The output generated would be a list containing values from 10 to 19.

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Passing start, stop and step arguments

When we pass all three arguments to the xrange() function, no default values would be considered for any parameters.

Let us take an example where we pass 10 as the start argument, 101 as the stop argument, and 10 as the step value. This will print numbers from 10 to 100 with a step size of 10.

n = xrange(10,101,10)

Printing the list sequence.

print(list(n))

The output list would be:

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

xrange() in python loops

Using xrange() in for loops, we can generate a sequence of numbers or use xrange() for indexing.

ITERATING:

xrange() can be used to print a sequence by iterating over a given range.

Example: To print the number sequence 10, 20, 30, … 100, we can pass the range of the sequence as arguments to xrange().

for i in xrange(10,101,10):
    print(i)

This shall print all the numbers between 10 and 101 with a step size of 10.

10
20
30
40
50
60
70
80
90
100

INDEXING:

If we have a given list, we can use xrange() to print all the list elements, bypassing the length of the list as an argument.

Let us consider a list of city names.

city = ['Seattle', 'New York', 'London', 'Sydney']

To print all the list elements, we shall pass the length of the list as the stop argument.

for i in xrange(len(city)):
    print(city[i])

The output will be:

Seattle
New York
London
Sydney

REVERSE:

We can also use xrange() to print the reverse of a given string or number. For that, we will have to assign the length of the string or number to start the argument, -1 to stop, and -1 as the step size.

If we have a variable that contains the string value ‘Programming,’ we can use xrange() to print the reverse of it.

a = 'Programming'
for i in xrange(len(a),-1,-1):
    print(a[i:i+1])

The output would be:

g
n
i
m
m
a
r
g
o
r
P

xrange() vs range() in python

The function range() is available in python version 2 and python version 3, but xrange() is available only in python version 2. It was removed from version 3. Both the functions serve the same use and have the same functionality. But the main characteristic difference between the two is that xrange() returns an xrange object, and range() returns a list object.

range() generates a static list during runtime, whereas xrange() does not generate a static list. xrange() will only store one item in memory and generate values based on the demand. Because of this feature, xrange() occupies less memory compared to range() function. Hence xrange() is more preferable when memory is a constraint. xrange() is also faster than range() because xrange() evaluates the generator object only when required.

If you have large ranges that have to be iterated, using xrange() is preferable as it occupies less memory.

But if you want to build a compatible program with both python 2 and python 3, then range() should be used.

Also, Read

FAQ’s on xrange python

Q. Can you use xrange in python 3?

A. No, xrange() function is not available available in python 3. xrange() is instead available in python 2. In python 3, the function range() has the same functionality as xrange(), and it an alternative for the same.

Q. Why “NameError: global name ‘xrange’ is not defined in Python 3” error appears?

A. The above NameError appears because the xrange() function is not present in the python 3 versions. Instead of xrange(), range() is available in python 3 and hence range() should be used. That shall resolve the error.


That was everything about xrange(). If you have any questions, do let us know in the comments.

Till then, Happy Learning!

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Michael Howitz
Michael Howitz
3 years ago

Python 2 is dead, please update the article so it is applicable to Python 3 in the first place instead of mentioning it only in the Q&A section.

Pratik Kinage
Admin
3 years ago
Reply to  Michael Howitz

Thank You for your valuable feedback.

Yes, I’ve updated the article with a note at the start of it. As for the range() function in Python 3, we’re about to release a separate post on it soon.

Best Regards,
Pratik