Python’s range and xrange | avinash.vora
It’s a subtle difference. For the sake of simplicity, let’s omit the the optional start
and step
parameters in both functions. range
returns exactly what you think: a list of consecutive integers, of a defined length beginning with 0. xrange
, however, returns an “xrange object”, which acts a great deal like an iterator. If you’ve spent anytime with iterators, this difference should make sense. Here’s an example:
range(1000000) xrange(1000000)
range(1000000)
will return a list of one million integers, whereas xrange(1000000)
will return (what is essentially) an iterator sequence. Indeed, xrange
supports iteration, whereas range
does not. The overall benefit is minimal, because xrange
(in the words of the Python manual) “still has to create the values when asked for them,” but at each call, it consumes the same amount of memory regardless of the size of the requested list. At extremely large values, this is a major benefit over range
. Another benefit is also apparent: if your code is going to break
out while traversing over a generated list, then xrange
is the better choice as you are going to consume less memory overall if you break
.
Read full article from Python’s range and xrange | avinash.vora
No comments:
Post a Comment