How to sort a list in reverse order in Python? You can use the reverse=True
on built-in sorted()
function in Python to sort a list in reverse order. This method will return a new list after sorting a list. Alternatively, you can also use the sort()
function with reverse=True
to sort the list in reverse order, this updates the sort in place, meaning that it will modify the original list and you won’t need to create a new list.
1. Quick Examples of Sorting Lists in Reverse Order
If you are in a hurry, below are some quick examples of sorting lists in reverse order.
# Below are quick examples
# Example 1: sorted() - list in reverse order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
sorted_tech = sorted(technology, reverse=True)
# Example 2: sort() the list in reverse order
technology.sort(technology, reverse=True)
# Example 3: Sort the list in reverse order
technology.sort(technology, reverse=True)
2. Using sorted() to Order List in Reverse
The sorted() function with reverse=True
in Python is used to sort a sequence (such as a list, tuple) in reverse order. The function returns a new sorted list, leaving the original sequence unchanged.
2.1 Sort Strings in Reverse Order Example
Here is an example of sorting lists of strings in reverse order.
# Sort list in reverse order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
print(technology)
sorted_tech = sorted(technology, reverse=True)
print(sorted_tech)
# Output
# ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
# ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop']
2.2 Sort Numbers in Reverse Order
Use param reverse=True
to sort list of numbers in reverse order. Here is an example,
# Sort list of numbers in reverse order
numbers = [30,20,10,70,50,0]
print("Original: ",numbers)
sort_numbers = sorted(numbers, reverse=True)
print("Sorted:",sort_numbers)
# Output:
# Original: [30, 20, 10, 70, 50, 0]
# Sorted: [70, 50, 30, 20, 10, 0]
3. Using sort() to Order List in Reverse
Similarly, you can also use the list.sort() function to order a list in reverse order. it also takes reverse as a parameter to specify whether the list should be sorted in ascending (False) or descending (True) order. The default is reverse=False. To sort in reverse use reverse=True
.
3.1. Using sort() to Reverse Order Example
Here is an example of how you can use the sort
method to order a list of strings in reverse order.
# Sort list in reverse order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
print(technology)
technology.sort(technology, reverse=True)
print(technology)
# Output
# ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
# ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop']
3.2 Sort Numbers in Reverse Order
To sort a list of numbers in reverse order use the sort()
method with the reverse argument set its value to reverse=True
.
# Sort list of numbers by reverse order
numbers = [30,20,10,70,50,0]
print("Original: ",numbers)
numbers.sort()
print("Sorted:",numbers)
# Output:
# Original: [30, 20, 10, 70, 50, 0]
# Sorted: [70, 50, 30, 20, 10, 0]
4. Sort a List with Numbers as String in Reverse Order
If you have a list with a number but in string type and wanted to sort in reverse order, first you need to convert it to int type by using key=int to sort() or sorted() functions.
Note that, here the sorted list will be of string type but the data is numerically sorted by ascending order.
# Sort list of strings with numbers
# Using sorted()
numbers = ["30","20","10","70","50","0"]
print("Original: ",numbers)
sprt_numbers = sorted(numbers, key=int, reverse=True)
print("Sorted:",sprt_numbers)
# Using sort()
numbers = ["30","20","10","70","50","0"]
print("Original: ",numbers)
numbers.sort(key=int, reverse=True)
print("Sorted:",numbers)
# Output:
# Original: ['30', '20', '10', '70', '50', '0']
# Sorted: ['70', '50', '30', '20', '10', '0']
Conclusion
In this article, I have explained how to sort a list in reverse order, First, I have covered using sorted() function and then list.sort(). To sort in reverse, both these functions take reverse=True as an argument.
Happy Learning !!
Related Articles
- How to Sort a List of Tuples in Python
- Python Sort List Descending
- Python Sort Dictionary by Key
- Python Sort Array Values
- Python Sort List of Numbers or Integers
- Python Sort List Alphabetically
- Sort using Lambda in Python
- How to Sort List of Strings in Python
- How to Sort Dictionary by Value in Python
- Sort Set of Values in Python
- Python Sort List of Lists