range and xrange in Python
In this post, you will learn about two methods of Python range() and xrange() and the differences between them. Both are used to generate a list of integers within the given range. These two inbuilt functions will come helpful while dealing with loops, conditional statements etc., i.e., where you have to loop or iterate through a list of integers/characters etc.
Difference between range() and xrange()
In Python 2.x, there are two functions available to generate a list of integers.
- range()
- xrange()
But, there is no xrange in Python 3. It is because the range() function in Python 3 is just a re-implementation of the xrange() function of python 2. It actually works the same way as the xrange does. If you want to write code that will run on both Python 2 and Python 3, you should use the range() function. The core functionality of both functions is the same, but there are differences in terms of return type, memory consumption, and speed.
Syntax of range and xrange
Range: range(start, stop, step)
Xrange: xrange(start, stop, step)
Here, start is the starting value of your list of integers, which is optional. If it is not defined, then 0 will be considered as the default start value. The stop is the value before which your list of integers will end, it is required. And, the step is the value by which your list of integers will be incremented. It is optional, the default value is 1.
Examples of range() and xrange()
x = range(1, 100, 5)
y = xrange(10, 100, 10)
print("x is : ", x)
print("y is : ", y)
Output of the above code-
('x is : ', [1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96])
('y is : ', xrange(10, 100, 10))
Difference between range and xrange in terms of return type
The range() function returns a list of integers, whereas the xrange() function returns a generator object. The given code demonstrates the difference between range() vs xrange() in terms of return type.
# initializing x variable with range()
x = range(1,1000)
# initializing y variable with xrange()
y = xrange(1,1000)
# getting the type of x
print("The return type of range() is : ")
print(type(x))
# getting the type of y
print("The return type of xrange() is : ")
print(type(y))
Output of the above code-
The return type of range() is :
<type 'list'>
The return type of xrange() is :
<type 'xrange'>
Difference between range and xrange in terms of memory consumption
Clearly range() consumes more memory than xrange() as it returns a list of integers while the xrange() returns the xrange object. So, the variable storing the range created by range() takes more memory as compared to the variable storing the range using xrange(). The following example demonstrates this.
import sys
# initializing x variable with range()
x = range(1,1000)
# initializing y variable with xrange()
y = xrange(1,1000)
# getting the size of x
print ("The size allotted using range() is : ")
print (sys.getsizeof(x))
# getting the size of y
print ("The size allotted using xrange() is : ")
print (sys.getsizeof(y))
Output of the above code-
The size allotted using range() is :
8064
The size allotted using xrange() is :
40
Difference between range and xrange in term of operation
As we know, the range() function returns a list of integers, so all the basic operations, like addition, subtraction, and multiplication can be performed on range(). But, we cannot perform these operations on xrange() as the return type is the xrange object. The given code demonstrates the difference between range() and xrange() on the basis of operation.
# initializing x variable with range()
x = range(1,1000)
# initializing y variable with xrange()
y = xrange(1,1000)
# applying slice operation on range()
print ("The list after slicing using range is : ")
print (x[2:6])
# applying slice operation on xrange()
print ("The list after slicing using xrange is : ")
print (y[2:6])
Output of the above code -
The list after slicing using range is :
[3, 4, 5, 6]
The list after slicing using xrange is :
Traceback (most recent call last):
File "main.py", line 13, in <module>
print (y[2:6])
TypeError: sequence index must be integer, not 'slice'
Difference between range and xrange in term of speed
The xrange() is faster in implementation than range(). As the xrange() evaluates the generator object only when required and not when the generator function is created.
Related Articles
zip function in Python
Python multiline string
Prettytable in Python
Multiply all elements in list Python
Count consonants in a string Python
Python dict inside list
Find the stop words in nltk Python
Python heapq
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Text Detection and Extraction using OpenCV and OCR
Python Random shuffle() Method
Fibonacci Series In Python | Python Program To Print
File Handling in Python
Python - XML to JSON
Python XML to Dictionary
Serialize Python dictionary to XML