in Technology by
What is the difference between xrange and range in Python?

1 Answer

0 votes
by

xrange() and range() are quite similar in terms of functionality. They both generate a sequence of integers, with the only difference that range() returns a Python list, whereas, xrange() returns an xrange object.

So how does that make a difference? It sure does, because unlike range(), xrange() doesn't generate a static list, it creates the value on the go. This technique is commonly used with an object type generators and has been termed as "yielding".

Yielding is crucial in applications where memory is a constraint. Creating a static list as in range() can lead to a Memory Error in such conditions, while, xrange() can handle it optimally by using just enough memory for the generator (significantly less in comparison).

for i in xrange(10):    # numbers from o to 9
    print i       # output => 0 1 2 3 4 5 6 7 8 9

for i in xrange(1,10):    # numbers from 1 to 9
    print i       # output => 1 2 3 4 5 6 7 8 9

for i in xrange(1, 10, 2):    # skip by two for next
    print i       # output => 1 3 5 7 9

<em style='-webkit-font-smoothing:subpixel-antialiased; background-color:#f8fcfd; border:0px; box-sizing:border-box; color:#7e868e; font-family:"Source Sans Pro",Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"; font-size:15px; margin:0px; padding:0px; text-rendering:optimizelegibility; vertical-align:baselin

Related questions

0 votes
    Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it ... xrange(0, 20): Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Our core application changed from Python 2.6 to Python 2.7 maybe to Python 3 in later release. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    What makes using range() to make the process of generating a quadrillion values in a instant of time, I am amazed by ... o += 1 return Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    What are the Difference between staticmethod and classmethod in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    What is the difference between Python Arrays and lists?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
    What is the difference between tuples and lists in Python?...
asked Nov 23, 2020 in Technology by JackTerrance
0 votes
    A filter that passes all frequencies lying outside a certain range, while it attenuates all frequencies between the two ... EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    I have a dataframe that looks like this: from to datetime other ---------------------------------- ... !! Thank you so much in advance! Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    A subnet is a range of IP addresses in a __________. 1. Cloud Service 2. VNet 3. DNS Server 4. VM role instance...
asked Sep 23, 2021 in Technology by JackTerrance
0 votes
    What is the range of the output values for a sigmoid function? (1)[0,.5] (2)[-inf,+ inf] (3)[0,inf] (4)[0,1]...
asked May 22, 2021 in Technology by JackTerrance
0 votes
    The __________ was a huge marketplace of Dark Web specifically famous for selling of illegal drugs & narcotics as well as you can ... Road 2) Cotton Road 3) Dark Road 4) Drug Road...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    Which of the following input control is used for input fields that should contain a value from a range of numbers in Web Form 2.0? A - week B - time C - number D - range...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    How can you pick a random item from a range?...
asked Nov 25, 2020 in Technology by JackTerrance
+1 vote
    A subnet is a range of IP addresses in a...
asked Oct 20, 2020 in Technology by JackTerrance
...