Python

How to Use Xrange in Python

In Python, xrange is a commonly used function that gives a series of numbers from a given range. In Python 2, there is a function that returns an xrange object. When we need to iterate through a loop, we use the xrange function. As a result, the object created by xrange is mostly utilized for indexing and iteration. Keep in mind that the xrange method is only supported in Python 2 at this time. Because Python 2 is no longer supported, we recommend using Python 3 and the range() method instead of xrange(). The range() and xrange() methods can be used for loops to iterate a specified number of times, let’s say 10 times or 5 times. Although Python 3 does not provide an xrange function, the range function works identically to the xrange function in Python 2.

If you want to develop programs that can be executed on both Python 2 and Python 3, you should utilize the range method. The range() returns a range object (a type of iterable), while xrange() returns a generator object that can only be used to loop through integers. The only specific range is presented on demand, leading to the term “lazy evaluation”. Both are used in various ways and have different qualities. The return type, memory, operation usage, and performance are all factors to consider. Let’s discuss each factor with a corresponding example for better understanding.

Example 1

Here’s some Python code that compares range() with xrange() in terms of the return type. First, we have initialized range() and xrange() with “one” and “two”, respectively. Finally, we put the “one” and “two” types to the test:

one = range(20000)
two = xrange(20000)
print ("Return type of range() is given below: ")
print (type(one))
print ("Return type of xrange() is given below: ")
print (type(two))

Here you can see the return type of range() and xrange():

Example 2

Now, we are going to discuss another factor, and that is memory. The variable holding the range produced by range() takes up more memory than the variable containing the range created by xrange(). This is because range() gives a list, whereas xrange() gives an xrange() object. The following Python code compares range() with xrange() in terms of memory. We used range() to initialize “one” and an xrange to initialize “two”. After that, we used the function sys.getsizeof to check the size of “one” and “two”. The system module in Python has a function called sys.getsizeof() that returns the memory size of the object in bytes. Rather than the memory consumption that the object references, the memory usage of the item is considered. Because this method is platform-specific, it returns the expected results when built-in objects are supplied to it. Otherwise, third-party extensions may provide incorrect results. The result reveals that range() consumes more memory, whereas xrange() consumes less:

import sys
one = range(20000)
two = xrange(20000)
print ("The size using range() is : ")
print (sys.getsizeof(one))
print ("The size using xrange() is : ")
print (sys.getsizeof(two))

This is the result of the previous code:

Example 3

Because range() produces a list, it can be used with any operation that can be applied to a list. However, because xrange() returns an xrange object, actions related to lists cannot be performed on it, which is a drawback. This code compares range() with xrange() in terms of operations. We have used range() and xrange() to initialize “one” and “two”, identical to the previous examples. Then, we used range() and xrange() to test the slice operation and printed the results. As can be seen, the xrange() throws an error:

one = range(1,6)
two = xrange(1,6)
print ("After slicing with range, the list looks like this: : ")
print (one[2:5])
print ("After slicing with xrange, the list looks like this: : ")
print (two[2:5])

Here, you can refer to the attached result of the previous code example:

Because it examines only the generating object that includes only the values required by slow evaluation, xrange() is faster to implement than range(). Remember before executing the programs listed above: If you want to write code that works in both Python 2 and Python 3, use range() instead of the xrange method, which is deprecated in Python 3. The range() is faster when iterating in the same sequence numerous times. Range() will have genuine integer objects, whereas xrange() will have to reconstruct the integer object every time.

Conclusion

Python has two routines or functionalities for producing lists or sometimes a range of integers. These can be used for loops. The two functions that we are talking about are xrange and range. Only if you are using Python 2.x and Python 3 will the range() and xrange() comparison be useful. It is because Python 3.x’s range() method is merely a re-implementation of Python 2.x’s xrange() method. It has the same functionality as the xrange. In terms of functionality, xrange and range are essentially the same. They both provide you the option of generating a list of integers that you may use any way you want. Range and xrange are identical except range produces a Python list object, whereas xrange provides an xrange object. We learned about range() and xrange in this session. We have also included some sample programs to show you how to put these programs and methods into practice on your own. We hope you found this article helpful. Check out more Linux Hint articles for tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content