Python’s range and xrange

range and xrange are very different in Python; do you use them correctly? I see a lot of code where the author seemingly didn’t know the difference. At first glance, there doesn’t seem to be one at all—in fact, when I started hacking in Python, I used them interchangeably because I couldn’t figure out what the difference was, and in all the code I was writing at the time, swapping one for the other made no conceivable difference.

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.

I’d love to see any cool xrange iterator tricks if you’d like to share them.

—★—

« Remap Caps Lock | Poor branding »