How to reverse a range in Python (with examples)
Tags: python, programming
Published: 2023-02-04 14:41:29

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/reverse-a-range-in-python) for a 💯 user experience. _~reza_

Sometimes you need to reverse a range in Python to iterate over a list of items in reverse order. This quick guide explores two popular methods of reversing ranges (or any iterator) in Python.

1. <u>Reverse a range using `reversed()`</u>
2. <u>Generate a reversed range</u>

Before we start, let’s quickly review Python’s `range()` function. A range object in Python is a sequence of numbers you can use with a for loop to repeat a set of instructions a specific number of times.

Python’s `range()` function makes it super easy to generate range objects. The range function accepts three parameters: `start`, `stop`, and `step`.

With these parameters, you can define where the sequence begins and ends. And thanks to the `step` parameter, you can even choose how big the difference will be between one number and the next.

For instance, to generate a range from `0` to `10` with a step of `2`, you call the `range` function like so:

```python
items = range(0, 10, 2)

print(items)
# output: range(0, 10, 2)

# Convert them into a list
print(list(items))
# output: [0, 2, 4, 6, 8]
```

Using ranges with for loops is straightforward:

```python
for i in range(0, 10, 2):
    print(i)
```

The above code generates the following output:

```
0
2
4
6
8
```

## Reverse a range using `reversed()`

Alright, now let's see how we can reverse a range in Python to iterate over it in reverse order.

Luckily, Python makes it easy to do this with the `reversed()` function.  

The `reversed()` function takes a range (or any iterator supporting the sequence protocol) and returns a reversed-iterator object. 

You can use `reversed()` function in conjunction with the `range()` function in `for` loops to iterate over a sequence in reverse order:

```python
for i in reversed(range(0, 10, 2)):
   print(i)
```

And the output would be:

```
8
6
4
2
0
```

You can also use the `reversed()` function with a list. In this case, the returned value will be a `list_reverseiterator` object:

```python
my_list = [1, 2, 3, 4, 5] 

for i in reversed(my_list):
    print(i)
```

## Generate a reversed range

You can also use the `range()` function to generate a sequence of numbers in reverse order. 

In this case, the `start` argument is the highest number in the range, and the `stop` parameter is the lowest. Obviously, the `step` parameter should be a negative value like `-1`.

To create a range from `10` to `0` with a step of `2`, we can do it like so:

```python
for i in range(10, 0, -2):
   print(i)
```

And to create a reversed list with negative numbers:

```python
for i in range(-10, 0, 2):
   print(i)
```

Output:

```
-10
-8
-6
-4
-2
```

Alright, I think that does it! I hope you found this quick guide helpful.

Thanks for reading

---

**❤️ You might like:**

- [TypeError: missing 1 required positional argument: 'self' (Fixed)](https://www.decodingweb.dev/python-typeerror-missing-1-required-positional-argument-self)
- [Unindent does not match any outer indentation level error in Python (Fixed)](https://www.decodingweb.dev/python-indentationerror-unindent-does-not-match-any-outer-indentation-level-fix)
- [AttributeError module 'DateTime' has no attribute 'strptime' (Fixed)](https://www.decodingweb.dev/attributeerror-module-datetime-has-no-attribute-strptime-python)
- [How back-end web frameworks work?](https://www.decodingweb.dev/backend-web-frameworks)
- [Your master sword: a programming language](https://www.decodingweb.dev/your-master-sword-a-programming-language)