To sort a list in descending order using the sort()
method with parameter reverse=True
. This method will order the list of strings in place, meaning that it will modify the original list and you won’t need to create a new list. You can also use the sorted() function to sort a list in descending order, this returns a new list after sorting. In this article, I will explain the python sort list descending by using the sort() method and sorted() function with examples.
1. Quick Examples of Sort List Descending
If you are in a hurry, below are some quick examples of the python sort list descending.
# Below are the quick examples
# Example 1: Sort the list of alphabets in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
technology.sort(reverse=True)
# Example 2: Use Sorted() strings in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
technology = sorted(technology, reverse=True)
# Example 3: Sort a list of strings in descending order
def myFunc(e):
return len(e)
technology = ['Hadoop','Spark','Pandas','Pyspark']
technology.sort(reverse=True, key=myFunc)
# Example 4: Sort a list of Integers in descending order
numbers = [4, 5, 9, 2, 7, 11]
numbers.sort(reverse=True)
# Example 5: Use sorted() to descending order
numbers = [4, 5, 9, 2, 7, 11]
numbers = sorted(numbers, reverse=True)
# Example 6: Sort string by integer value use key as int
strings = ['12','34','5','26','76','18','63']
strings.sort(reverse=True, key = int)
# Example 7: Sorting using user-defined order
def get_length(val):
return val[1]
# Create list of tupple
myList = [(3, 5), (6, 8), (2, 7)]
myList.sort(key=get_length, reverse=True)
2. Sort List of Strings in Descending Order
To sort a list of strings in descending order, you can use the sort()
method with the reverse argument set its value to True
. Descending order is the opposite of ascending order where elements are arranged from highest to lowest value.
# Sort the list of alphabets in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
technology.sort(reverse=True)
print(technology)
# Output:
# ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hadoop']
3. Use Sorted() Strings in Descending Order
You can also use sorted() a list of strings in descending order, you can pass the reverse=True
argument to the sorted()
function. Descending order is the opposite of ascending order where elements are arranged from highest to lowest value (for string Z to A).
# Use Sorted() strings in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
technology = sorted(technology, reverse=True)
print(technology)
# Output
# ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hadoop']
4. Sort List of Strings by Length
You can pass the function to the key to sort a Python list in a custom order. For example, pass key=myFunc
to sort a list of strings by length. You can also use the reverse parameter to specify whether the sort should be in descending order. The following example sorts the list of items based on their length in descending order.
# Sort a list of strings in descending order
def myFunc(e):
return len(e)
technology = ['Hadoop','Spark','Pandas','Pyspark']
technology.sort(reverse=True, key=myFunc)
print(technology)
# Output
# ['Pyspark', 'Hadoop', 'Pandas', 'Spark']
5. Sort a List of Integers in Descending Order
You can use the sort()
method and pass the reverse=True
argument to sort a list of integers in descending order.
# Sort a list of Integers in descending order
numbers = [4, 5, 9, 2, 7, 11]
numbers.sort(reverse=True)
print(numbers)
# Output
# [11, 9, 7, 5, 4, 2]
Alternatively, you can use the built-in sorted()
function and pass the reverse=True
argument.
# Use sorted() to descending order
numbers = [4, 5, 9, 2, 7, 11]
numbers = sorted(numbers, reverse=True)
print(numbers)
# Output
# [11, 9, 7, 5, 4, 2]
Similarly, you can sort the list of strings in descending order by their integer value using the key argument of the sort method. The key argument is set to int, which means that the sort function will use the integer value of each string as the key for sorting.
# Sort string by integer value use key as int
strings = ['12','34','5','26','76','18','63']
strings.sort(reverse=True, key = int)
print(strings)
# Output
# ['76', '63', '34', '26', '18', '12', '5']
6. Use User-defined Descending Order
You can use the sort()
function on the list List and sorts it in descending order based on the values returned by the get_length()
function. This function takes a tuple as an argument and returns the second element of the tuple. The sort()
method is used to sort the list of tuples, and the key
argument is set to the get_length()
function, which tells the sort method to use the second element of each tuple as the key for sorting. The reverse=True
argument is used to sort the list in descending order.
# Sorting using user-defined order
def get_length(val):
return val[1]
# Create list of tupple
myList = [(3, 5), (6, 8), (2, 7)]
# Sorts the array in descending order to
# get_length element
myList.sort(key=get_length, reverse=True)
print(myList)
# Output
# [(6, 8), (2, 7), (3, 5)]
Conclusion
In this article, I have explained how to sort lists in descending order in python, first, I have covered using list.sort()
function and python built-in function sorted()
.
Happy Learning !!
Related Articles
- How to Sort a List of Tuples in Python
- Python Sort Dictionary by Key
- Python Sort Array Values
- Python Sort List in Reverse Order
- 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