Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is xrange in Python

Understanding xrange in Python

When you're starting out with programming, you may come across different ways to create sequences of numbers in Python. One such way, which was used in Python 2, is xrange. Let's take a closer look at what xrange is and how it works.

What is xrange?

xrange was a built-in function in Python 2, and it was used to generate a sequence of numbers, much like the range function. However, there's a key difference between the two that's important for beginners to understand: xrange generates the numbers on-demand or lazily, while range creates a list of numbers in memory.

Imagine you're at a party and you're in charge of handing out numbered tickets to guests. Using range would be like pre-printing all the tickets and handing them out as guests arrive. On the other hand, xrange would be like writing the number on a blank ticket each time a new guest arrives. With xrange, you're creating each number only when you need it, which can be more efficient, especially when dealing with a large number of guests (or in programming terms, a large sequence of numbers).

How does xrange work?

xrange returns an "xrange object", which generates the numbers on-demand as you iterate over it. This is different from range, which returns a list of numbers. Here's a simple example to illustrate how xrange works:

for i in xrange(5):

This code would output:


Each time the loop runs, xrange generates the next number in the sequence, starting from 0 and going up to, but not including, 5.

The Benefits of Using xrange

The main benefit of using xrange is that it can save memory. If you're generating a large range of numbers, storing all of those numbers at once can take up a lot of memory. Since xrange generates numbers one at a time, it can be much more memory-efficient.

Imagine you're making a shopping list. If you write down every single item you need to buy before you go to the store, your list might be very long and take up a lot of space. But if you have a mental list and only think of each item as you're walking down the aisles, you're using your memory more efficiently. That's similar to how xrange saves memory by generating numbers only when they're needed.

When to Use xrange

You should consider using xrange when you're working with a large range of numbers and you want to be memory-efficient. However, it's important to note that xrange is only available in Python 2. In Python 3, range has been updated to do what xrange did in Python 2, and the xrange function has been removed.

xrange vs range in Python 2

In Python 2, range creates a list of numbers, which can be indexed and has a certain size in memory. xrange, on the other hand, creates an object that generates each number in the sequence as you loop through it.

Here's a comparison using code:

# Using range
number_list = range(5)
print(number_list)  # Output: [0, 1, 2, 3, 4]

# Using xrange
number_generator = xrange(5)
print(number_generator)  # Output: xrange(5)

Notice how range returns a list of numbers, while xrange returns an object. You can't see the numbers generated by xrange until you iterate over them.

Transition to Python 3

As you may know, Python 2 has reached the end of its life, and Python 3 is now the standard. In Python 3, the range function behaves like xrange did in Python 2. So, if you're using Python 3, you don't need to worry about xrange at all.

Here's how you would use range in Python 3:

for i in range(5):

The output will be the same as the xrange example given earlier, but now you're using Python 3's range function, which is memory-efficient.

Practical Examples of Using range in Python 3

Let's look at some practical examples of how you would use range in Python 3, which now incorporates the functionality of the old xrange.

Looping a Specific Number of Times

for i in range(3):
    print("This is loop number", i)

Creating a List of Numbers

number_list = list(range(10))

Using Steps in a Range

even_numbers = list(range(2, 11, 2))

In this example, range starts at 2, ends before 11, and increments by 2, so it generates even numbers.

Conclusion: Embracing the Evolution of Python

As a beginner in programming, understanding the evolution of functions like xrange to range in Python is part of the learning journey. While xrange is a concept of the past for Python 3 users, its principles live on in the current range function. The move from xrange to range is like trading in an old, reliable car for a newer model that has all the features of the old one but performs better and is more efficient.

Remember, Python is designed to be an easy-to-read language that favors simplicity. The consolidation of xrange into range is a reflection of that philosophy, making it easier for new programmers to write efficient code without getting bogged down by too many choices. As you continue your programming adventure, you'll find that Python's simplicity is one of its greatest strengths, helping you to turn your focus from the intricacies of the language to the problem-solving and creativity that programming is all about.