How to check if a list is in descending order in Python

Python | Check if list is sorted or not

The sorted operation of list is essential operation in many application. But it takes best of O(nlogn) time complexity, hence one hopes to avoid this. So, to check if this is required or not, knowing if list is by default sorted or not, one can check if list is sorted or not. Lets discuss various ways this can be achieved.

Method #1 : Naive method
The simplest way to check this is run a loop for first element and check if we could find any smaller element than it after that element, if yes, the list is not sorted.




# Python3 code to demonstrate
# to check if list is sorted
# using naive method
# initializing list
test_list = [1, 4, 5, 8, 10]
# printing original list
print ("Original list : " + str(test_list))
# using naive method to
# check sorted list
flag = 0
i = 1
while i < len(test_list):
if(test_list[i] < test_list[i - 1]):
flag = 1
i += 1
# printing result
if (not flag) :
print ("Yes, List is sorted.")
else :
print ("No, List is not sorted.")

Output :

Original list : [1, 4, 5, 8, 10] Yes, List is sorted.

Method #2 : Using sort()
The new list can be made as a copy of the original list, sorting the new list and comparing with the old list will give us the result if sorting was required to get sorted list or not.




# Python3 code to demonstrate
# to check if list is sorted
# using sort()
# initializing list
test_list = [10, 4, 5, 8, 10]
# printing original list
print ("Original list : " + str(test_list))
# using sort() to
# check sorted list
flag = 0
test_list1 = test_list[:]
test_list1.sort()
if (test_list1 == test_list):
flag = 1
# printing result
if (flag) :
print ("Yes, List is sorted.")
else :
print ("No, List is not sorted.")

Output :



Original list : [10, 4, 5, 8, 10] No, List is not sorted.

Method #3 : Using sorted()
Using the similar analogy as the above method, but does not create a new space, but just a momentary space for that time and hence useful, shorter and faster method than above.




# Python3 code to demonstrate
# to check if list is sorted
# using sorted()
# initializing list
test_list = [1, 4, 5, 8, 10]
# printing original list
print ("Original list : " + str(test_list))
# using sorted() to
# check sorted list
flag = 0
if(test_list == sorted(test_list)):
flag = 1
# printing result
if (flag) :
print ("Yes, List is sorted.")
else :
print ("No, List is not sorted.")

Output :

Original list : [1, 4, 5, 8, 10] Yes, List is sorted.

Method #4 : Using all()
Most elegant, pythonic and faster way to check for sorted list is the use of all(). This uses the similar method as naive, but use of all() make it quicker.




# Python3 code to demonstrate
# to check if list is sorted
# using all()
# initializing list
test_list = [9, 4, 5, 8, 10]
# printing original list
print ("Original list : " + str(test_list))
# using all() to
# check sorted list
flag = 0
if(all(test_list[i] <= test_list[i + 1] for i in range(len(test_list)-1))):
flag = 1
# printing result
if (flag) :
print ("Yes, List is sorted.")
else :
print ("No, List is not sorted.")

Output :

Original list : [9, 4, 5, 8, 10] No, List is not sorted.

How to check if a list is in descending order in Python




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list

“check if list is in descending order python” Code Answer


how to sort a list descending python
python by Last_Guardian on Apr 10 2020 Comment
4
Add a Grepper Answer

  • how to sort list in descending order in python
  • python sort list in reverse
  • how to sort a list of dictionary by value in descending order?
  • print list in reverse order python
  • python sort list in reverse order
  • check if list is in ascending order python
  • how to reverse a list in python
  • reorder list python
  • dataframe sort values descending
  • python sort descending
  • print python reverse list
  • sort and reverse list in python
  • list reverse method in python

  • python sort descending
  • sort list
  • how to sort in descending order in python
  • sort in python
  • sort reverse python
  • sorted reverse python
  • descending order python
  • sort list in python
  • how to sort a list descending python
  • sort list reverse python
  • python sort list reverse
  • sort a list python
  • sort list python descending
  • python sort list descending
  • how to sort a list in python in descending order
  • python list sort reverse
  • how to sort list in descending order in python
  • python sort a list
  • how to sort list in python in descending order
  • sorted in descending order python
  • reverse sort list python
  • python list reverse sort
  • sorted decreasing order python
  • sort list descending python
  • python bisect descending list
  • sort python
  • sort(reverse=true) python
  • python descending order function
  • acsending and descending list order
  • python sort list from big to small
  • how to sort a list in python
  • sort a list in python in descending order
  • how to arrange list in ascending order in python
  • how to sort array in descending order python
  • sort list descending in python
  • list sort python descending
  • how to sort a list in reverse order in python
  • how to reverse sort a list in python
  • sort an array in descending order python
  • python sorting greatest to least
  • how to sort in reverse order python
  • how to sort ascending descending in python
  • sort descending list python
  • list.sort reverse python
  • sort the updated list in descending order python
  • how to make an array list to descending order
  • how to keep list in sorted order python
  • how to put a list in descending order python
  • how to sort a list in python in descending order without function
  • python sort reverse list
  • reverse sort a list
  • python list descending
  • sort desc list python
  • reverse sort a python list
  • descending list sort
  • order list descending python
  • python order descending
  • sorted python descending on key of list
  • sort list with descending order
  • how to print sorted list in descending order
  • python sort in reverse order
  • how to sort values in python in descending
  • python print in descending order
  • sorted list descending order python
  • descending order program in python
  • sort elements in descending order python
  • how to reverse sort a list in python using pandas
  • how to sort an numpy list python in descending order
  • ascending and descending order python
  • python descending sort
  • .sort reverse python
  • sort array in descending order in python
  • .sort(list.descending
  • how to reverse sort the list python
  • how to sort a list in reverse in python
  • list.sort in reverse
  • sort a list in python reverse
  • how to sort a list in descending order inside a method python
  • sort descending array python
  • how to sort list in descending order in python according to a field in list
  • sort array reverse python
  • sorted reverse in python
  • sorted desc python
  • sorted()reverse=true
  • order list high to low python
  • python sort list to corrct seuqnce
  • sort list order
  • how to sort array in reverse order python
  • pythons built it sort function
  • what is sort method doing in python
  • desc() function in python
  • sorted python reversed
  • python sort in reverse
  • sort a list inplace python
  • how to sort a list from highet to lowest in python
  • python order list from least to greatest
  • sort number in descending order python
  • python sort list base on name
  • list sorted function in python
  • sort numbers descending order python
  • python arrange list by values
  • sort function in python
  • python list.sort la
  • sort descending a list in python
  • python sort a list decreasing
  • descending order list
  • python descending order
  • python sort lsit descending order
  • how to sort an array in descending order in python
  • how to display list items in descending order in python
  • python sorted list descending
  • how to reverse sort list in python
  • sort python list in descending order
  • descending order fun python
  • python check if list is in descending order
  • ascending and descending list order
  • order by ascending in python
  • sorted() reverse python
  • icic cdpyhton list assendign
  • sorted descending python
  • sorted in decending order python
  • how to sort a list in decreasing order in python
  • python3 sort descending
  • python sort array in desc order
  • ascending order program in python
  • how to sort a list in python in ascending order
  • sort descending python
  • sort list in descending order python
  • python sort reverse
  • sort list in descending order in python
  • sort a list in descending order python
  • python list sort descending
  • print in ascending order python
  • python sort in descending order
  • sort list in descending order
  • sort array in descending order python
  • sort a list in python
  • python ascending order
  • sort array descending python
  • sort and reverse list in python
  • how to arrange a list in descending order in python
  • sort in python descending
  • sort by descending python
  • sorted python reverse
  • python sort numbers descending
  • sort a list in increasing order python
  • sort python descending
  • sort with function python
  • how to sort a list in descending order in python
  • python sort descending order
  • list.sort python
  • how to short a list in python
  • python sorted reverse
  • how to print in descending order in python
  • how to sort a list in descending order in a method python
  • sorted reverse
  • sorting in descending order in python
  • sorting a list descending and ascending
  • reverse sort list in python
  • reverse sort a list in python
  • python reverse sort order
  • how to sort a list in descending order
  • python sort array descending
  • descending sort python
  • sorted descending in python
  • sorted with list in python
  • sort from smallest to largest python lists
  • python sort list backwards
  • sort list in reverse order python
  • can we check if a list is in descending order in python
  • list descending python
  • list in descending order
  • sort() python descending
  • phyton sort list reverse
  • how to sort python array in descending order
  • orderby.descending.to list
  • descending using sort function in python
  • how to arrange list in descending order in python
  • how to sort python list in descending order
  • how to make a list in descending order python
  • how to change order of a list to descending
  • sort an array list in descending order
  • how to reverse sort a list using sorted in python
  • how to sort a list in python decreasing order
  • sort a list of number descending python
  • sort a list in descending order python without using inbuilt function
  • how to sort the list in the descending order of the numerical values of the list.
  • sort list ascending order and descending order python
  • python sor in descending
  • sort list descending
  • how to sort list reverse
  • set the list in descending order python
  • sort the following list into decreasing order in python
  • python sort reverse order
  • sorting descending in python
  • sort the list in descending order
  • python array descending order
  • how to make descending order in python
  • how to sort and reverse a list in python
  • sort a list descending order in python
  • sort array in descending python
  • sort descending inbuilt py function
  • arrange an array in descending order python
  • python sorted descending order
  • descending sort array in python
  • how to sort in descending order python
  • sort list python from bigger to smaller
  • sorting words in desending python
  • how to sort by descending order in python
  • python sort list largest to smallest
  • .sort python array
  • sort a list by value python o(n)
  • list sort function in python
  • sort() list python
  • python list sort in descending order
  • python sort strings in reverser order
  • python sort high to low
  • sort pyhton
  • how to order a list in desending order in python
  • ascending order sorting python
  • how to sort a list from highest to lowest in python
  • how to sort descending python
  • python reverse sort list
  • sort array from highest to lowest python
  • .sort python
  • sorting function in python
  • python print order of list
  • in descending order python
  • pthon list sort
  • python list descending order
  • python sort list by values descending
  • descending list
  • to sort data in descending order in python list
  • how to sort a list ascending and descending order in python
  • check if list is in descending order python
  • sorting an array in descending order in pyhtin
  • how to change a list into descending order
  • sort a list descending python
  • python sorrt descending
  • sort a list in descending order in python
  • sorting descending order in python
  • accednig order list
  • list in python is ordered
  • to print ascending order and descending order in python
  • python sorting a listing
  • python reverse sorted
  • sort a list in decreasing order python
  • list reverse sort python
  • ascending order of a list in python
  • ascending order list
  • what the order of elements in a list python
  • sort in descending order list python
  • sort in descending order python
  • python list sort
  • sort list python
  • descending order in python
  • how to sort a list from largest to smallest python
  • reverse sort python
  • sort array python descending
  • sort in reverse order python
  • sort in python list
  • python sort
  • how to sort a list in python
  • python reverse sort
  • print a list in descending order python
  • sorting a list from largest to smallest
  • list sorting in python
  • sort high to low python
  • descending sort in python
  • python order by descending
  • how to sort a list in descending order python
  • list.descending
  • how to sort array in descending order in python
  • how to sort a list in ascending order in pythonn
  • how to check if a list is in descending order in python
  • sorted python descending
  • sort descending order python
  • list descending order python
  • reverse order sort python
  • python array sort descending
  • descending order list python
  • python sort list in reverse order
  • sort in reverse python
  • how to sort by asc python
  • python sort opposite
  • how to print descending order in python
  • list in descending order python
  • python sort list in descending order
  • display the list in descending order
  • order a list python from high to low
  • how to sort list descending order
  • python sort array in descending order
  • sorting list in python
  • python order list
  • python sort list by value descending
  • how to sort values in descending order in python
  • check if list is sorted python descending order
  • best way to sort an list as descending in python
  • sort descending in python
  • print list in descending order python
  • reverse sort in python list
  • if a list is descending
  • array.sort in python 3 descending order
  • sorting list in descending order in python
  • how to reverse sort a list python
  • a list in descending order
  • how to sort descending order python
  • descending order sort in python
  • sort list in reverse
  • how to sort a part of list in python decreasing order
  • how to return list elements descending order in python
  • check order of descending list python
  • sort a list in descending order
  • how to sort list in descending order
  • how to sort an array in descending order python
  • python sort list in decreasing order
  • sort the list in descending order in python
  • set the list in descending order
  • how to create list value change in descending order in python
  • how to see descending order in python
  • how to sort a list in python descending
  • order list in decending order python
  • python change order of list descending
  • how to sort descending order in python
  • arrange list in descending order
  • make a list descending in python
  • order items by descending python list
  • sort list descending order\
  • sorted order descending python
  • python to get sort a list by reverse order
  • python sort and reverse list
  • find sorting of list in python
  • list sort python function
  • object showing in alphabetical order python
  • sort a fucntion in python
  • how to ascending order list in python
  • arrange the list of list in increasing order
  • sorted key python reverse
  • how to sort a list backwards in python
  • sort in python
  • easiest way to sort a list python
  • python how to sort strings in reverse order
  • l sort in python reverse
  • ascending or descending order + python
  • put numbers in size order python
  • how to sort descending in python
  • python sorted vs sort
  • python function to sort a list
  • sort function reverse python
  • python sort by greatest list
  • sorting a list in python desc
  • sort python decreasing
  • how to get the acending order in list python
  • sort decreasing python
  • arrange in descending order in python list
  • .sort descending python
  • how to sort one list in ascending order and descending
  • order descending python
  • how to make a list in python goes in descending order
  • sort a list in descending order python def
  • reverse sorting of list in python
  • how to arrange the list in descending order in python
  • descending order of list in python
  • sortby descending python
  • function to sort in descending order python
  • how to sort the items in a list from low to high python
  • how to sort in ascending order python
  • how to make list in ascending order in python
  • sort list from large to small
  • sort the array in descending order in python
  • sorty array python
  • invirse sort python
  • how to sort array in python
  • list sort in python reverse
  • python sorted reverse list
  • sorted descending pyhton
  • how to keep the list in ascending order python
  • how to descending order in python

Python List sort() - Sorts Ascending or Descending List

The list.sort() method sorts the elements of a list in ascending or descending order using the default < comparisons operator between items.

Use the key parameter to pass the function name to be used for comparison instead of the default < operator. Set the reverse parameter to True, to get the list in descending order.

Syntax:

list.sort(key=None, reverse=False)

Parameters:

  1. key: (Optional) A function that extracts a comparison key from each list element while sorting.
  2. reverse: (Optional) If true, the sorted list will be reversed. By default, it is False.

Return Value:

No return value. It sorts the list itself.

The following example demonstrates the sort() function on numeric lists.

Example: Sort Numeric List
Copy
nums = [1, 5, 3, 4, 2, 10, 6, 8, 7, 9] nums.sort() print('List in Ascending Order: ', nums) nums.sort(reverse=True) print('List in Descending Order: ', nums)
Output
List in Ascending Order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List in Descending Order: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

The sort() method can also be used on character lists.

Example: Sort Char List
Copy
al = ['a','d','e','c','b'] al.sort(reverse=True) print('List in Descending Order: ', al) al.sort() print('List in Ascending Order: ', al)
Output
List in Descending Order: ['e', 'd', 'c', 'b', 'a'] List in Ascending Order: ['a', 'b', 'c', 'd', 'e']

The following example sorts the string list in alphabetical order.

Example: Sort String List
Copy
cities = ['Mumbai', 'London', 'Paris', 'New York'] cities.sort() print('List in Ascending Order: ', cities) cities.sort(reverse=True) print('List in Descending Order: ', cities)
Output
List in Ascending Order: ['London', 'Mumbai', 'New York', 'Paris'] List in Descending Order: ['Paris', 'New York', 'Mumbai', 'London']

What is the sort() method in Python?

This method takes a list and sorts it in place. This method does not have a return value.

In this example, we have a list of numbers and we can use the sort() method to sort the list in ascending order.

my_list = [67, 2, 999, 1, 15] # this prints the unordered list print("Unordered list: ", my_list) # sorts the list in place my_list.sort() # this prints the ordered list print("Ordered list: ", my_list)

If the list is already sorted then it will return None in the console.

my_list = [6, 7, 8, 9, 10] # this will return None because the list is already sorted print(my_list.sort())

The sort() method can take in two optional arguments called key and reverse.

key has the value of a function that will be called on each item in the list.

In this example, we can use the len() function as the value for the key argument. key=len will tell the computer to sort the list of names by length from smallest to largest.

names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"] print("Unsorted: ", names) names.sort(key=len) print("Sorted: ", names)

reverse has a boolean value of True or False.

In this example, reverse=True will tell the computer to sort the list in reverse alphabetical order.

names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"] print("Unsorted: ", names) names.sort(reverse=True) print("Sorted: ", names)

🔹 Purpose and Use Cases

With the sort() method, you can sort a list in either:

  • Ascending Order
  • Descending Order

This method is used to sort a list in place, which means that it mutates it or modifies it directly without creating additional copies, so remember:

You will learn more about mutation in this article (I promise!), but for now it's very important that you know that the sort() method modifies the list, so its original version is lost.

Because of this, you should only use this method if:

  • You want to modify (sort) the list permanently.
  • You don't need to keep the original version of the list.

If this fits your needs, then the .sort() method is exactly what you are looking for.

🔸 Syntax and Arguments

Let's see how you can call .sort() to take advantage of its full power.

This is the most basic call (with no arguments):

If you don't pass any arguments, by default:

  • The list will be sorted in ascending order.
  • The elements of the list will be compared directly using their values with the < operator.

For example:

>>> b = [6, 3, 8, 2, 7, 3, 9] >>> b.sort() >>> b [2, 3, 3, 6, 7, 8, 9] # Sorted!

Custom Arguments

To customize how the sort() method works, you can pass two optional arguments:

  • Key
  • Reverse

Let's see how they change the behavior of this method. Here we have a method call with these two arguments:

Before explaining how they work, I would like to explain something that you probably noticed in the diagram above – in the method call, the names of the parameters have to be included before their corresponding values, like this:

  • key=
  • reverse=

This is because they are keyword-only arguments. If you are passing a custom value for them, their names have to be specified in the method call, followed by an equal sign = and their corresponding values, like this:

Otherwise, if you try to pass the arguments directly as we normally do for positional parameters, you will see this error because the function will not know which argument corresponds to which parameter:

TypeError: sort() takes no positional arguments

Reverse

Now that you know what keyword-only arguments are, let's start with reverse.

The value of reverse can be either True or False:

  • False means that the list will be sorted in ascending order.
  • True means that the list will be sorted in descending (reverse) order.

💡 Tip: By default, its value is False – if you don't pass any arguments for this parameter, the list is sorted in ascending order.

Here we have a few examples:

By default, reverse is False# List of Integers >>> b = [6, 3, 8, 2, 7, 3, 9] >>> b.sort() >>> b [2, 3, 3, 6, 7, 8, 9] # List of Strings >>> c = ["A", "Z", "D", "T", "U"] >>> c.sort() >>> c ['A', 'D', 'T', 'U', 'Z']

💡 Tip: If the elements of the list are strings, they are sorted alphabetically.

To specify that reverse is True, so the list has to be sorted in descending (reverse) order.# List of Integers >>> b = [6, 3, 8, 2, 7, 3, 9] >>> b.sort(reverse=True) >>> b [9, 8, 7, 6, 3, 3, 2] # List of Strings >>> c = ["A", "Z", "D", "T", "U"] >>> c.sort(reverse=True) >>> c ['Z', 'U', 'T', 'D', 'A']

💡 Tip: Notice how the list is sorted in descending order if reverse is True.

Key

Now that you know how to work with the reverse parameter, let's see the key parameter.

This parameter is a little bit more detailed because it determines how the elements of the list are be compared during the sorting process.

Basic Syntax

The value of key is either:

  • None, which means that the elements of the list will be compared directly. For example, in a list of integers, the integers themselves can be used for the comparison.
  • A function of one argument that generates an intermediate value for each element. This intermediate value is calculated only once and it's used to make the comparisons during the entire sorting process. We use this when we don't want to compare the elements directly, for example, when we want to compare strings based on their length (the intermediate value).

💡 Tip: By default, the value of key is None, so the elements are compared directly.

For example:

Let's say that we want to sort a list of strings based on their length, from the shortest string to the longest string. We can pass the function len as the value of key, like this:

>>> d = ["aaa", "bb", "c"] >>> d.sort(key=len) >>> d ['c', 'bb', 'aaa']

💡 Tip: Notice that we are only passing the name of the function (len) without parenthesis because we are not calling the function. This is very important.

Notice the difference between comparing the elements directly and comparing their length (see below). Using the default value of key (None) would have sorted the strings alphabetically (left), but now we are sorting them based on their length (right):

What happens behind the scenes? Each element is passed as an argument to the len() function, and the value returned by this function call is used to perform the comparisons during the sorting process:

This results in a list with a different sorting criteria: length.

Here we have another example:

Another interesting example is sorting a list of strings as if they were all written in lowercase letters (for example, making "Aa" equivalent to "aa").

According to lexicographical order, capital letters come before lowercase letters:

>>> "E" < "e" True

So the string "Emma" would come before "emily" in a sorted list, even if their lowercase versions would be in the opposite order:

>>> "Emma" < "emily" True >>> "emma" < "emily" False

To avoid distinguishing between capital and lowercase letters, we can pass the function str.lower as key. This will generate a lowercase version of the strings that will be used for the comparisons:

>>> e = ["Emma", "emily", "Amy", "Jason"] >>> e.sort(key=str.lower) >>> e ['Amy', 'emily', 'Emma', 'Jason']

Notice that now, "emily" comes before "Emma" in the sorted list, which is exactly what we wanted.

💡 Tip: if we had used the default sorting process, all the strings that started with an uppercase letter would have come before all the strings that started with a lowercase letter:

>>> e = ["Emma", "emily", "Amy", "Jason"] >>> e.sort() >>> e ['Amy', 'Emma', 'Jason', 'emily']

Here is an example using Object-Oriented Programming (OOP):

If we have this very simple Python class:

>>> class Client: def __init__(self, age): self.age = age

And we create four instances:

>>> client1 = Client(67) >>> client2 = Client(23) >>> client3 = Client(13) >>> client4 = Client(35)

We can make a list that references them:

>>> clients = [client1, client2, client3, client4]

Then, if we define a function to get the age of these instances:

>>> def get_age(client): return client.age

We can sort the list based on their age by passing the get_age function an an argument:

>>> clients.sort(key=get_age)

This is the final, sorted version of the list. We use a for loop to print the age of the instances in the order that they appear in the list:

>>> for client in clients: print(client.age) 13 23 35 67

Exactly what we wanted – now the list is sorted in ascending order based on the age of the instances.

💡 Tip: Instead of defining a get_age function, we could have used a lambda function to get the age of each instance, like this:

>>> clients.sort(key=lambda x: x.age)

Lambda functions are small and simple anonymous functions, which means that they don't have a name. They are very helpful for these scenarios when we only want to use them in particular places for a very short period of time.

This is the basic structure of the lambda function that we are using to sort the list:

Basic Structure of a Lambda Function

Passing Both Arguments

Awesome! Now you know to customize the functionality of the sort() method. But you can take your skills to a whole new level by combining the effect of key and reverse in the same method call:

>>> f = ["A", "a", "B", "b", "C", "c"] >>> f.sort(key=str.lower, reverse=True) >>> f ['C', 'c', 'B', 'b', 'A', 'a']Sort the list in reverse order as if the strings were in all lowercase.

These are the different combinations of the arguments and their effect:

The Order of Keyword-Only Arguments Doesn't Matter

Since we are specifying the names of the arguments, we already know which value corresponds to which parameter, so we can include either key or reverse first in the list and the effect will be exactly the same.

So this method call:

Is equivalent to:

This is an example:

>>> a = ["Zz", "c", "y", "o", "F"] >>> a.sort(key=str.lower, reverse=True) >>> a ['Zz', 'y', 'o', 'F', 'c']

If we change the order of the arguments, we get the exact same result:

>>> a = ["Zz", "c", "y", "o", "F"] >>> a.sort(reverse=True, key=str.lower) >>> a ['Zz', 'y', 'o', 'F', 'c']