Python has two handy functions for creating lists, or a range of integers that assist in making for loops.

These functions are xrange and range. But you probably already guessed that! πŸ™‚

The Difference Between xrange and range in Python

Before we get started, let's talk about what makes xrange and range different.

For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and xrange returns an xrange object.

What does that mean? Good question! It means that xrange doesn't actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. If you want to read more in depth about generators and the yield keyword, be sure to checkout the article Python generators and the yield keyword.

Okay, now what does that mean? Another good question. That means that if you have a really gigantic range you'd like to generate a list for, say one billion, xrange is the function to use. This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a MemoryError and crash your program. It's a memory hungry beast.

That being said, if you'd like to iterate over the list multiple times, it's probably better to use range. This is because xrange has to generate an integer object every time you access an index, whereas range is a static list and the integers are already "there" to use.

Alright, now on to the good stuff.

How to Use Python's range and xrange

So how do we use range and xrange? Here is the simplest example:

Great! Simple enough. Note that you could also use range in place of xrange here, but I personally like xrange better. Maybe it's that sexy "x" in the front. πŸ™‚

Alright, explanation time. The functions xrange and range take in three arguments in total, however two of them are optional. The arguments are "start", "stop" and "step". "start" is what integer you'd like to start your list with, "stop" is what integer you'd like your list to stop at, and "step" is what your list elements will increment by.

Python's xrange and range with Odd Numbers

Say we wanted only odd numbers. Here's what we could do:

We told Python that we would like the first element to be one, the last element to be one less than ten, and that we'd like each element to go up by two. Really simple stuff.

Python's xrange and range with Negative Numbers

Alright, so what about if we want a negative list?

Simple. Here's an example:

That's it! All we have to do is change the "start", "stop" and "step" to negative numbers. Awesome. Please note that you must do it this way for negative lists. Trying to use xrange(-10) will not work because xrange and range use a default "step" of one.

Another Python xrange and range Example

Here's one more example of even numbers between 100 and 120:

And that's pretty much it for xrange and range. If you would like a more technical explanation of what these two functions do, please consult the Python docs for xrange and range.

Note that if Β "start" is larger than "stop", the list returned will be empty. Also, if "step" is larger that "stop" minus "start", then "stop" will be raised to the value of Β "step" and the list will contain "start" as its only element.

Here's a clearer picture:

Deprecation of Python's xrange

One more thing to add. In Python 3.x, the xrange function does not exist anymore. The range function now does what xrange does in Python 2.x, so to keep your code portable, you might want to stick to using range instead. Of course, you could always use the 2to3 tool that Python provides in order to convert your code, but that introduces more complexity.

The reason why xrange was removed was because it is basically always better to use it, and the performance effects are negligible. So Python 3.x's range function is xrange from Python 2.x.

You can find the PEP document hereΒ for reasoning why it was removed.

Over and out, soldier. Over and out.

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

About The Author

  • me

    thaks Duuuude!

  • Jim

    Title = “The Difference Between xrange and range in Python”
    Examples using “range” for comparison = None
    Clarity of “Difference” < Potential

    • Jackson Cooper

      range() uses the same syntax, the difference is implementation.

    • ‘/usr/test’

      Wow now! that makes me feel clued in better than I was ever aware.

    • Jim

      I made the Title =” . . . .
      message a month ago. However the english as a second language, bot like message from a Jim 8 hours ago was not me.

  • kasu

    thank you bro!! πŸ˜€

  • Max Humphrey-calou

    Awesome and concise explanation πŸ˜€

  • eric_rice66

    Pretty neat explanation! Thanks mehn.

  • RF Kill

    Nice explanation

  • Stephen Huynh

    Thank you!!. The explanation is easy to understand.

  • Parker Watkins

    Should have put the deprecation subheading at the start of the article. If xrange is depacated in Python 3 it’s usage becomes less essential.

  • Antara Bahursettiwar

    That was very helpful! Thank you! πŸ™‚

  • satish jasti

    Thanks a lot Joey