You are currently viewing Python Sort List in Reverse Order

How to sort a list in reverse order in Python? In Python, you can use the sorted() function to sort a list in reverse order. The sorted() function returns a new sorted list without modifying the original list. 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.

Advertisements

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.


# Quick examples of sorting lists in reverse order

# Example 1: Sort the list in reverse order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
sorted_list = sorted(technology, reverse=True)

# Example 2: Sort list in reverse order
technology.sort(reverse=True)

# Example 3: Sort the numbers in reverse order
numbers = [30,20,10,70,50,0]
sorted_numbers = sorted(numbers, reverse=True)

# Example 4: Sort the numbers in reverse order in-place
numbers.sort(reverse=True)

# Example 5: Sort list of numbers by reverse order
numbers.sort()

# Example 6: Sort list of strings with numbers
# Using sorted() function
numbers = ["30","20","10","70","50","0"]
sprt_numbers = sorted(numbers, key=int, reverse=True)

# Example 7: Sort list of numbers (as strings) 
# In reverse order in-place
numbers.sort(key=int, 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

You can sort a list of strings in reverse order using the sorted() function. For instance, the sorted() function is used to create a new sorted list of strings in reverse order, and the reverse=True parameter is passed to indicate that the sorting should be done in descending order.


# Create a list of strings
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
print("Original strings:\n",technology)

# Sort the list in reverse order
sorted_list = sorted(technology, reverse=True)
print("Sorted list in reverse order:\n", sorted_list)

Yields below output.

python sort list reverse

2.2 Sort Numbers in Reverse Order

Use param reverse=True to sort list of numbers in reverse order. For example, it creates a list of numbers and then sorts the list in reverse order using the sorted() function with the reverse=True parameter. The sorted_numbers variable contains the sorted version of the original list in reverse order.


# Create a list 
numbers = [30,20,10,70,50,0]
print("Original list:\n",numbers)

# Sort the numbers in reverse order
sorted_numbers = sorted(numbers, reverse=True)
print("Sorted numbers in reverse order:\n", sorted_numbers)

# Output:
# Original:  [30, 20, 10, 70, 50, 0]
# Sorted: [70, 50, 30, 20, 10, 0]

Yields below output.

python sort list reverse

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

If you want to sort the original list in reverse order in-place, you can use the sort() method with the reverse=True parameter. This modifies the original list (technology) directly and sorts it in reverse order. The reverse=True argument in the sort() method indicates that the sorting should be done in descending order.


# Create a list of strings
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
print("Original strings:\n",technology)

# Sort list in reverse order
technology.sort(reverse=True)
print("Sorted in reverse order:\n",technology)

# Output
# Original strings:
#  ['Java', 'Hadoop', 'Spark', 'Pandas', 'Pyspark', 'NumPy', 'Hyperion']
# Sorted in reverse order:
#  ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop']

3.2 Sort Numbers in Reverse Order

To sort numbers in reverse order using the sort() method, you can use the reverse=True parameter. This code modifies the original list (numbers) directly and sorts it in reverse order using the sort() method with reverse=True.


# Create a list of numbers
numbers = [30, 20, 10, 70, 50, 0]
print("Original list:\n", numbers)

# Sort the numbers in reverse order in-place
numbers.sort(reverse=True)
print("Numbers sorted in reverse order:\n", numbers)

# Output:
# Original list:
#  [30, 20, 10, 70, 50, 0]
# Numbers sorted in reverse order:
#  [70, 50, 30, 20, 10, 0]

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.sort()
print("Numbers sorted in reverse order:\n", numbers)

# Output:
# Numbers sorted in reverse order:
#  [0, 10, 20, 30, 50, 70]

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 list:",numbers)
sprt_numbers = sorted(numbers, key=int, reverse=True)
print("Sorted:",sprt_numbers)

# Sort list of numbers (as strings) 
# In reverse order in-place
numbers = ["30","20","10","70","50","0"]
print("Original list: ",numbers)
numbers.sort(key=int, reverse=True)
print("Sorted:",numbers)

# Output:
# Original list:  ['30', '20', '10', '70', '50', '0']
# Sorted: ['70', '50', '30', '20', '10', '0']

Frequently Asked Questions on Python Sort List in Reverse Order

How can I sort a list in reverse order in Python?

You can sort a list in reverse order in Python using either the sorted() function or the sort() method of a list. Both approaches accept the reverse=True parameter.

Can I sort a list of strings in reverse order?

You can use the same techniques to sort a list of strings in reverse order. Just use the reverse=True parameter with sorted() or sort().

What is the difference between sorted() and sort() for sorting in reverse order?

The sorted() function returns a new sorted list, leaving the original list unchanged. On the other hand, the sort() method sorts the list in-place, modifying the original list directly.

How does the key parameter work when sorting numbers as strings in reverse order?

When sorting numbers represented as strings, use the key=int parameter to convert the strings to integers for sorting. This ensures the sorting is done based on numerical values.

Are there any other alternatives for sorting a list in reverse order?

Another approach is to use slicing to reverse the sorted list. For example, sorted_list[::-1] will give you the elements in reverse order without modifying the original list.

Can I sort a list of custom objects in reverse order?

You can define a custom comparison function or use the key parameter to specify the attribute based on which the objects should be sorted in reverse order.

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

References