gotchapythonCriticalCanonical
What is the difference between range and xrange functions in Python 2.X?
Viewed 0 times
functionsxrangeandbetweenthedifferencerangewhatpython
Problem
Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about
for i in range(0, 20):
for i in xrange(0, 20):Solution
In Python 2.x:
-
-
In Python 3:
-
range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.-
xrange is a sequence object that evaluates lazily.In Python 3:
rangedoes the equivalent of Python 2'sxrange. To get the list, you have to explicitly uselist(range(...)).
xrangeno longer exists.
Context
Stack Overflow Q#94935, score: 1071
Revisions (0)
No revisions yet.