7th Sep 2022 7 minutes read How to Reverse a Range in Python Juliano Luiz Faccioni python reverse range Find out how to reverse a range in Python and learn more about the range(), reversed(), and sorted() functions. Early in your Python journey, you’ll start working with sequences and ranges. At some point, you may wonder ‘How would I get these values in the reverse order?’. If you think about it, we reverse ranges constantly: when we sort files in our computer from ascending to descending order, we effectively reverse their original order. As it turns out, there are multiple ways of reversing a range in Python. In this article, we will learn a few different options. We will start by introducing the range() function and work our way through some examples. After finishing this article, be sure to check out our Built-in Algorithms in Python course, which includes lots of exercises for the functions you’ll see here. Let’s get started! Basic Usage of the range() Function Let’s start with the basics: Python’s range() function is used to produce a sequence of increasing numbers (or decreasing numbers, as we will see below). This may not sound very exciting at first, but range() is useful in many contexts — some of which we will explore later in this article. Let’s create a range() object in Python: r = range(10) print(r) # output: # range(0, 10) But wait, where are the numbers? A range object doesn’t actually show its elements to you until you explicitly request them. So to access the numbers, you’ll need to turn the range into a sequence. You can use the list() or tuple() functions for this (and you can learn more about lists and tuples in this article): r = range(4) my_list = list(r) print(my_list) # output: # [0, 1, 2, 3] Another way to access the numbers in the range is to iterate over it in a for loop. If the name “for loop” confuses you, we recommend checking out our Learn Programming with Python track, which goes over — and well beyond — all of Python’s basic syntax. Below is a simple example of using the range() function in a for loop: for n in range(5): print(n) # output: # 0 # 1 # 2 # 3 # 4 Despite the simplicity of these examples, we can already detect some properties of the range() function. For example, provided a number n, range() produces integers from 0 up to n-1. In the example above, we passed 5 to the range() function, so the last number printed was 4 (since 5 - 1 = 4). Sometimes, you may not care about the numbers in the range. The range() function can be used in for loops simply as a way to perform an action repeatedly: for n in range(10): # repeat the code below 10 times print("Hello, World!") # output: # "Hello, World!" # "Hello, World!" # ... Parameters of the Python range() Function Until now, we’ve simply passed a single number to range(), but this function actually accepts up to three arguments: start, stop, and step. The start represents the initial value in the range. By default, this value is 0. The stop represents the end point of the range. This value has no default (i.e. it must always be provided). Note that the range goes up to the stop value, but does not include it. A range with a stop of 7 will have the number 6 as its last element. The step represents the number of elements to “skip” at every iteration. Its default value is 1, meaning that no values are skipped. A step value of 3 means that the range includes one element for every 3 numbers (e.g. 0, 3, 6, 9, …). If you pass two arguments to the range function, they are interpreted as the start and the stop (in this specific order). To provide a step argument, you need to provide the start and stop as well. Here are some examples of the start, stop, and step parameters in action: # start = 0 (default), stop = 3, step = 1 (default) r1 = range(3) print(list(r1)) # output: # [0, 1, 2] # start = 2, stop = 6, step = 1 (default) r2 = range(2, 6) print(list(r2)) # output: # [2, 3, 4, 5] r3 = range(4, 10, 2) # start = 4, stop = 10, step = 2 print(list(r3)) # output: # [4, 6, 8] As you can see, there are ways to define virtually any range of numbers you may want. But what if you need these values in the reverse order? The reversed() Function The reversed() function is used to… well, reverse a sequence! You simply provide any sequence to reversed() and it will store its elements in the reverse order. Here’s how it looks: my_list = [1, 5, 10, 2] for n in reversed(my_list): print(n) # output: # 2 # 10 # 5 # 1 As you can see, the elements in my_list were printed in reverse order. Cool! Note that, just like with the range() function, you need to iterate over reversed objects – or use the list() or tuple() functions – to actually get their values: my_list = [1, 5, 10, 2] my_reversed_object = reversed(my_list) print(my_reversed_object) # not very useful! # output: # values = list(my_reversed_object) print(values) # here are the values! # output: # [2, 10, 5, 1] Reversing a Range Using range() and reversed() You may have seen this coming, but you can combine the range() and reversed() functions to reverse a range in Python. It works just like you would expect: for n in reversed(range(5)): print(n) # output: # 4 # 3 # 2 # 1 # 0 In the example above, range() has a default start of 0 and a stop of 5, so it goes from 0 to 4. But since it is wrapped in a reversed() function call, the elements are printed in reverse order. In short, that’s one way to reverse a range in Python! Reversing a Range Using a Negative Step Although using reversed() is one of the easiest ways to reverse a range in Python, it is far from being the only one. For instance, you can also pass a negative step argument into the range() function: for n in range(5, 0, -1): print(n) # output: # 5 # 4 # 3 # 2 # 1 See how it works? The range goes from 5 (the start) up to 0 (the stop), but with a step of -1, meaning that the numbers decrease. It might be a bit tricky to wrap your head around the negative step at first. Many people expect the start value to always be less than the stop. But since we’re dealing with negative steps, we want to start at a number that’s greater than the value where we will stop. When in doubt, always remember: if you see three arguments in a range() function call, they’re always the start, the stop, and the step, in this exact order! You can use values other than -1 for the negative step, too. This causes the range to skip a given amount of numbers in each iteration, just like with positive steps. In the example below, we jump downwards 10 values with each iteration: for n in range(100, 10, -10): print(n) # output: # 100 # 90 # 80 # ... Reversing a Range With the sorted() Function As a bonus, here’s another way to reverse a range: use the sorted() function with the parameter reverse=True. This will sort a sequence in reverse order. Since a range object is pretty much always sorted by definition, this effectively causes our range to be reversed. Check out the example below: r = range(5) sorted_values = sorted(r, reverse=True) print(sorted_values) # output: # [4, 3, 2, 1, 0] As promised, the resulting list represents the initial range of [0, 1, 2, 3, 4] in the reverse order. This is just a small sample of what you can do with the sorted() function. You can even write custom sort functions to go with it. This function is also present in our Built-in Algorithms in Python course, so be sure to check it out if you want to learn more! Reverse a Range in Python We went through several examples of how to reverse a range in Python. There are multiple options available, so choose whichever feels best in your code! And don’t stop here — if you want to keep on learning Python, we recommend checking out the Python Data Structures in Practice course. Your learning journey is just beginning! Tags: python reverse range