Kodeclik Blog

# How to reverse a range in Python

A range is a sequence of numbers like from 1 to 10. Reversing this range means to set it up so that the sequence starts from 10 and ends in 1. Let us learn how to do so in Python!

But first, why do we need ranges in Python? The most common use of ranges is to setup a for loop or other iteration. For instance, suppose we wish to print the squares of numbers from 1 to 10. We can use the range() function like so:

```
for x in range(1,11):
print(str(x) + " squared is " + str(x*x) + ".")
```

In this program we have a for loop and begins at 1 (the first argument of the range function) and incrementally updates this index till we exceed 11 (the second argument of the range function). Thus, do not confused by the 11 - this range starts at 1 and ends at 10. Within the for loop, we compute the square of the index and print a helpful message. The output is:

```
1 squared is 1.
2 squared is 4.
3 squared is 9.
4 squared is 16.
5 squared is 25.
6 squared is 36.
7 squared is 49.
8 squared is 64.
9 squared is 81.
10 squared is 100.
```

It is not necessary to use the range() function in a for loop. You can also do something simple like:

`print(range(1,11))`

The output will be:

`range(1, 11)`

which is not very descriptive. You can apply the list() function on the range to render the range’s elements in a list:

`print(list(range(1,11)))`

The output will now be:

`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`

Let us now learn how to reverse a range in Python.

The first idea is to specify a third argument, which is the step size (by default: 1) and this time makes the step size to be -1. Of course, you have to flip the start and ending values to make the range start at a higher value and count down to the lower value.

```
for x in range(10,0,-1):
print(str(x) + " squared is " + str(x*x) + ".")
```

In the program above note that we have inverted the first two arguments to range(). The second argument is made to be one less than what we need, and thus it is zero. Then we have given a third argument which specifies a negative step size. Running this program will give us the desired range in reverse:

```
10 squared is 100.
9 squared is 81.
8 squared is 64.
7 squared is 49.
6 squared is 36.
5 squared is 25.
4 squared is 16.
3 squared is 9.
2 squared is 4.
1 squared is 1.
```

The second way to reverse a range is to use the reversed() function which takes a sequence or range or any iterable object as input and returns it in reverse order:

```
print("Original range:")
print(list(range(1,11)))
print("Reversed range:")
print(reversed(range(1,11)))
```

The output is:

```
Original range:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Reversed range:
<range_iterator object at 0x7faa04aa7210>
```

What happened? We had forgotten to convert the range to a list for ease of printing. Let us update our program:

```
print("Original range:")
print(list(range(1,11)))
print("Reversed range:")
print(list(reversed(range(1,11))))
```

The output is now what we desired:

```
Original range:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Reversed range:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
```

A third way to obtain a reverse range is to first construct it the normal way and then use slicing operators to traverse the range in the opposite direction.

```
myrange = range(1,11)
print("Original range:")
print(list(myrange))
print("Reversed range:")
print(list(myrange[::-1]))
```

Note that in the last line we are using square brackets (“[“ and “]”) to index into the range variable called “myrange”. The step size of -1 is how you communicate to Python that you are traversing it in the opposite direction. The output is as we expected:

```
Original range:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Reversed range:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
```

We have learnt three different ways to reverse a range in Python. Which one is your favorite? For more on ranges, checkout our blogpost on different ways to convert a Python range to a list.

If you liked this blogpost, learn how to check for an empty string in Python.

For more Python content, checkout the math.ceil() and math.floor() functions! Also

learn about the math domain error in Python and how to fix it!

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.