If you want to know how to reverse the range of a list in Python, then this article is the best fit for you; in this article, we have solved the mystery of reverse range. So keep reading. 📖
Most programmers, while using the range, don’t understand its syntax in detail, and that’s why they face many problems. In this article, At first, we will explain in detail the range function, and later on, we will discuss different ways of reverse range in python. In this context, we have discussed reverse, reversed, and sorted functions. So without wasting any further time, dive deep 🏊 into the article.
Table of Contents
What is the range() Function in Python?
The range() is a built-in function in Python that generates a sequence of numbers within a given range. The most common use is to iterate a series of numbers using any loop. Range function works with integer value only if we pass float; it will throw a syntax error.
We have three syntaxes of range now; let’s discuss them one by one.
If we just pass one parameter, then it will range start from 0 and end on stop-1 in each iteration value of n is increased by 1. It is the default increment value of the range, for example:
for n in range(5): print(n)
0 1 2 3 4
2. range(start, stop)
If we pass starting and ending points, then the range will start from the given number and end on stop-1 in each iteration value of n is increased by 1 it is the default increment value of the range, for example:
for n in range(2,5): print(n)
2 3 4
3. range(start, stop, step)
If we pass three arguments to the range function, the range will start from the given starting number and end on stop – 1 in each iteration value is increased or decreased according to step value. For example:
for n in range(2,10,2): print(n)
2 4 6 8
How to Reverse Range in Python?
We have multiple ways to reverse range in python. The following are the four easiest ways to reverse the range function:
- Using the range function with a negative step
- Using the reverse function
- Using the reversed function
- Using the sort function
Method 1: Using the Range Function With a Negative Step
The easiest method of reversing the range is just the range function. As mentioned above, in its 3rd syntax, we can pass step value for increment or decrement. To reverse the range, we just pass arguments to reverse the order of the range. Now let’s understand this concept with the help of a simple example:
for n in range(10,0,-1): print(n)
10 9 8 7 6 5 4 3 2 1
Method 2: Using the Reverse Function
We can easily print the range with the help of the reverse function and list. The reverse function is used to reverse the order of the list. To do so, you have to follow these steps:
- Create range variable
- Convert range into a list
- Call list built-in function reverse()
- Print the updated list
Now let’s understand this with the help of a simple example:
# Create a range from 0 to 9 myrange = range(10) # convert the range into a list mylist = list(myrange) # Call list function reverse to reverse the range mylist.reverse() # Print reversed list on the screen print(mylist)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Method 3: Using the Reversed Function
We can reverse the range with the help of reversed function, which takes the sequence object(range, tuple, list, etc.) and returns the list in reversed order. Now let’s understand it with the help of an example:
# range(10) returns range from 0 to 9 # reversed function reverses the order of range for num in reversed(range(10)): print(num)
9 8 7 6 5 4 3 2 1 0
Method 4: Using the Sorted Function
We can use the sorted function to sort the range in descending order; this function takes an argument of the sequence and returns a sorted list.
The general syntax of this function is:
sorted(iterable, key, reverse)
In this syntax, we have to pass an iterable object, which can be a sequence of numbers or strings. The key is an optional parameter, a function that would serve as a key or a basis for sorting.
The reverse is also optional; if we don’t pass it default value is false means sorted will return ascending order list, and if we want descending order list, then we have to pass the true so the sorted function will return a reverse list. Now let’s understand this function with a simple example:
# range(5) returns range from 0 to 4 # pass range to sorted function and set reverse to true reverseRange = sorted(range(5), reverse=True) print(reverseRange)
[4, 3, 2, 1, 0]
To summarize the article on “how to reverse range in Python”, we discussed several ways; from all these ways, the simplest and easiest way is by giving a negative step value to reverse the range without calling any extra functions.
We went through different simple examples and provided you with the detail of reverse(), reversed(), and sorted() functions to solve the mystery of reverse range.
Here are the topics we have covered in this article:
- What is range function?
- Reverse range with the help of negative step value
- Reverse range with the help of reverse function
- Reverse range with the help of reversed function
- Reverse range with the help of sorted function
If you find this article helpful, share it with your coding mates and let us know in the comment section below 🔽which method helps you most to solve your mystery of reverse range.