Python xrange vs. range

Till the Python version 2.x, there were a total of two underlying methods used in this language to generate the list of integers within a provided range. The two methods are listed below:

range ()
xrange ()

Moving forward, with the latest version of Python (3 onwards), range () was withdrawn, and xrange () was then changed into range (). Now in Python 3, there is only one function for this method, i.e., range (). In Python 3, the range () function is just another way of implementing the older version of xrange() of python 2.x. Here, we will relate the two.

Xrange ()

The xrange () is used to create a number sequence like the range () function.


The syntax used to define xrange () is:


The function is used to define the range of numbers starting from (is included) till the end (is not included).


Following is the list of required parameters:

 Start: Starting position of the number sequence
 End: Ending position of the number sequence
 Step: The distinction between two consecutive numbers in the series.


In the following example, we will check the ways to define xrange.

Here, we will be specifying the end position only.

So, the end value is set as 5, and then we get the end position printed, as shown below:

Now, we will see the method of calling range, the syntax to call end will be:

>>> x = xrange(end)

Then we will get it printed.

We will get the range in the output, as shown above.

Now, we will define both the starting and endpoints. Here, the starting point is 2, and the ending point is 5. We then printed the starting and ending positions, as shown below:

After this, we will be creating a sequence of the numbers from our starting and ending points, i.e., 2 to 5.

>>> y = xrange(start,end)

Finally, we will check out the method of defining the starting point, the step, and the ending point. Once we have defined all three parameters; we will be calling them similar to the method shown below:

Now, to call the xrange for these three parameters, we will be using the following syntax:

>>> z = xrange(start,step,end)

Range ()

Range () is used to create a list and is a faster function for multiple iterations.


The following syntax is used:

>>> range(start,end,step)


For the first case, we will be defining the end value. The syntax used for this is:

>>> range(end)

So, in the example given below, we will be using 3 as the range’s end value. When we get it printed, it returns the values, excluding the end value.

In the subsequent example, we are using the example of describing the start and ending point. The value will begin from 1 and end at 10 (by excluding it). The starting point is included, but the ending point is omitted. The syntax is similar to the one given below:

>>> range (start, end)

So, we define the starting point and then the endpoint, which is 1 and 10, respectively.

Now, in the subsequent example, we will have the step function. The function that defines the gap between any two points within the sequence. The value will begin from 0 and end at 10 (by excluding it). The syntax used is given below:

>>> range (start,step,end)

The example is given below, where 2 is the step value.


range ()

It is faster if the iterations are to be done multiple times. range () has only the real-time integer object values. In terms of memory, it does not execute well.


It has to recreate the integer object every time. xrange() isn’t since it doesn’t support slices and list methods. xrange() takes the same amount of memory. So, as far as performance is concerned, especially when users are iterating over a larger range value, xrange() performs much better.

Similarities between Python 2 and Python 3 range and xrange

Python 2’s xrange has a descriptive representation in the form of the string, which is very similar to Python 3’s range object value.

The value of xrange() in Python 2 is iterable, so is rang() in Python 3.

xrange() and range() both have a step, end, and starting point values. In both of the cases, step is an optional field, so is the start value.

Both xrange of Python 2 and 3 support length that can be indexed in forward or reverse order. Here is an example of the same:

Dissimilarities between range() and xrange()

Since xrange() evaluates only the generator object with the values needed by the lazy evaluation, it is faster to implement over the range(). range () helps return the list and has all objects that can be used, whereas xrange() returns the objects associated with a list and cannot be applied to them so that we can count it as a disadvantage.

The variable used in the range () function stores the value of range and thus takes a lot of memory as compared to xrange() that only takes some of the memory due to the variables. range () returns a range object whereas, xrange() returns a generator object.

The range (1, 7, 2) function will return the output [1, 3, 5] and the input xrange(1, 7, 2) will produce the output [1, 3, 5]. That’s how we can assume that they are similar in the pattern.


range () and xrange() both have different characteristics, as discussed above. All comparisons mentioned in this tutorial, along with the examples, will be helpful for the readers to choose better their preferred method based on their requirements.

About the author

Aqsa Yasin

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.